A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/linux-unix/for-command-in-linux-with-examples/ below:

for command in Linux with Examples

for command in Linux with Examples

Last Updated : 27 Sep, 2024

Introduction

The for command in linux used in shell scripting to iterate over a set of values or perform set of tasks repeatedly. The for loop allows users to automate operations efficiently which is easier to maintain.

Syntax:
for NAME [in WORDS ... ] ; do COMMANDS; done

Here NAME takes the value of each item in the list.

WORDS contains a set of values.

COMMANDS contains a set of actions to perform.

Example:
for i in 0 1 2 3 4 
do
  echo "$i"
done
Output Options used with for loop :

option

Description

for var in *

Iterate through all files in the current directory

for var in {start..end}

Loop through a sequence of numbers or characters

for var in $(command)

Iterate through the output of a command

Iterating over files in a directory :
for file in *; do
  echo "$file"
done
Output

The above image kept for reference. There are so many rows for this output.

Iterating over a range of numbers :
for i in {1..5}; do
  echo "Number $i"
done
Output : Using command output :
for process in $(ps -e | awk '{print $1}'); do
  echo "Process ID: $process"
done
Output :

The above image kept for reference. There are so many rows for this output.

Options:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4