How to execute a loop in a bash script?

I have the following lines in a bash script on Linux:

...
mkdir max15
mkdir max14
mkdir max13
mkdir max12
mkdir max11
mkdir max10
...

how is the syntax to put them in a loop, so I don’t need to write numbers (15,14 ..)?

+3
source share
5 answers
+2
source

with bash, no need to use external type commands seqto generate numbers.

for i in {15..10}
do
 mkdir "max${i}"
done

or simply

mkdir max{01..15} #from 1 to 15

mkdir max{10..15} #from 10 to 15

let's say if your numbers are generated dynamically, you can use C style for loop

start=10
end=15
for((i=$start;i<=$end;i++))
do
  mkdir "max${i}"
done
+14
source

:

mkdir max{15..10} max0{9..0}

... , :

for i in $(seq [ <start> [ <step> ]] <stop>) ; do
     # you can use $i here
done

for i in {<start>..<stop>} ; do 
     # you can use $i here
done

for (( i=<start> ; i < stop ; i++ )) ; do
     # you can use $i here
done

seq [ <start> [ <step> ]] <stop> | while read $i ; do
     # you can use $i here
done

, $i , - |, -

+6
for a in `seq 10 15`; do mkdir max${a}; done

seq 10 15.

EDIT: I got used to this structure since many years. However, when I observed other answers, it is true that it is {START..STOP}much better. Now I have to get used to the creation of a directory is much better mkdir max{10..15}.

+4
source
for i in {1..15} ; do
    mkdir max$i
done
+1
source

Source: https://habr.com/ru/post/1771146/


All Articles