Skip to Content

10 Bash for Loop In One Line Examples

Bash For loop is used to execute a series of commands until a particular condition becomes false. Bash for loop in one line is very handy for Linux admins. 

Bash for loop in one line is a control structure that allows you to repeat a certain set of commands multiple times.

The syntax is:

for i in (list); do command1; command2; done.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!
  • # for i in 1 2 3 4 5 ; do echo “$i” ; done
  • # for i in {1..5} ; do echo “$i” ; done
  • # for i in {1..5..1};do echo “$i” ; done
  • # for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus; do echo $planet; done

 

Bash in Linux

Bash is a default shell now. It is very convenient. For example, they remember commands that we have typed and let us reuse those commands.

It also let us edit those commands, so they don’t have to be the same each time. And bash let us define our own command abbreviations, shortcuts, and other features.

Bash is programmable. We can write a bash script to handle our daily work. Whenever we find ourselves doing a task repeatedly, we should try to automate it by writing a shell script.

There are more powerful scripting languages, like Perl, Python, and Ruby, but the Linux shell bash is a great place to start. After all, we already know how to type commands.

What Is Bash in Linux?

 

Bash for loop C style In One Line with items

The “c style of bash for loop” is a for loop that uses the C programming language style. It has the following syntax:

for ((expression; expression; expression))
     do
    command
done

The first expression is used to initialize the loop. The second expression is used to test whether or not the loop should continue. The third expression is used to increment the loop counter.

This command will print the numbers 1 to 5 to the screen.

# for ((i=1;i<=5;i++));do echo $i;done

Bash For Loop In one line with Command Output

# for i in `seq 1 5`;do echo $i ;done

# for i in `cat test`;do dig $i +short ;done

# for i in `awk '{print $1}' test` ;do ping -c 2 $i ;done

 

The “`” symbol is used to create a subshell. The “`” symbol is used to execute the command that is contained within the quotes.

Here’s an example:

`date`

This command will display the current date and time on the screen.

seq command

The “seq” command is used to generate a sequence of numbers.Here’s an example: seq 100. This command will generate a sequence of numbers starting at 0 and ending at 100.

The “seq” command has the following syntax:

  • seq [OPTION]… LAST
  • seq [OPTION]… FIRST LAST
  • seq [OPTION]… FIRST INCREMENT LAST

 

The “LAST” parameter specifies the last number in the sequence. The “FIRST” parameter specifies the first number in the sequence. The “INCREMENT” parameter specifies the amount by which each number is increased.

for i in `cat test`;do dig $i +short ;done

The “cat” command is used to view the contents of a file. Here’s an example: cat filename.txt. This command will display the contents of the file filename.txt on the screen.

The “for i in `cat test`” command will execute the “dig” command for each line in the test file. The “dig” command is used to query DNS servers.

Bash For Loop In one Line with variables

The “$(command)” syntax is used to execute a command and insert the output of the command into a string.

Here’s an example.

echo "Hello $(whoami)"

This command will display “Hello root” on the screen.

# for i in $(cat test);do dig $i +short ;done

# a="a b c"
# for i in $a;do echo $i;done
a
b
c

# a=(a b c)
# for i in ${a[@]};do echo $i;done
a
b
c

# for i in $(seq 1 5);do echo $i ;done

Bash For Infinite Loop In one Line

# for (( ; ; )); do echo "Hello World!"; done

# while true; do echo "Hello World!"; done

# while :; do echo "Hello World!"; done

 

An infinite for loop is a for loop that never ends. Bash has two types of infinite for loops: the while loop and the for loop.

The while loop has the following syntax:

while [CONDITION]
do
command
done

The CONDITION parameter is used to test whether or not the loop should continue. If the CONDITION is true, then the commands in the loop will be executed. If the CONDITION is false, then the loop will exit.

Bash For Loop In One Line with Files

# for i in *; do echo "Found the following file: $i"; done

# for i in `cat filelist.txt`; do echo ${i}; done;

# cat filelist.txt | while read LINE; do echo "${LINE}"; done

 

If a line may include spaces better use a while loop.

Related:

Bash For Loop Examples In Linux

Linux Troubleshooting Guide:

Linux Learning Guide: