In bash, you can use a for
loop in a single line by using the following syntax:
1 2 3 |
for variable in list; do command1; command2; done |
Here’s an example that prints out the numbers from 1 to 5:
1 2 3 |
for i in 1 2 3 4 5; do echo $i; done |
This will output the following:
1 2 3 4 5 6 7 |
1 2 3 4 5 |
You can also use the seq
command to generate a range of numbers, like this:
1 2 3 |
for i in $(seq 1 5); do echo $i; done |
This will have the same effect as the previous example.
Note that the ;
characters are used to separate the different commands, and the do
and done
keywords mark the beginning and end of the loop, respectively.
Add comment