There is a need to write a simple bash script, in a loop from 1 to X, that executes certain code, but this way:

#!/bin/bash for c in {1...5} do #some code done 

does not work, and so:

 ... for (( i=1 ; i < 5 ; i++ )) ... 

also. What to do? Bash version 4.4-5.

  • for i in $(seq 1 4); do echo $i; done for i in $(seq 1 4); do echo $i; done - vp_arth
  • In the first version there should be two points - Alexey Ten

1 answer 1

try this:

 for num in 1 2 3 5 7 11 ; do echo $num ; done 

or so:

 for num in $(seq 1 10) ; do echo $num ; done 

UPD:

In the script you can do this:

 if [ $# -eq 0 ] ; then echo "to few args" && return 1; else for num in $(seq 1 $1) ; do echo $num ; done fi 

and then call:

 ./script.sh 5 >>> 1 2 3 4 5 
  • Option 2 came up, thanks. - Alexander Ketcher
  • @AlexKetcher, option added to UPD in response. - LXA