Bash /
LoopsBasic structurefor i in range ; do action done where range is a string containing the tokens to be acted upon. This makes Bash loops potentially very powerful since they can act on pretty much anything. You can have a mundane loop: for i in 1 2 3 4 5 ; do echo $i done -> 1 -> 2 -> 3 -> 4 -> 5 where if you want a larger sequence, you should see the seq function. The freedom in defining the range and the capability of Bash to act on strings instead of just numbers allows you to act on files (at your own risk!): for i in $( ls *.out ); do mv $i output/ done Note: This can be achieved with a simple Single line
Using variables in a loopfor i in `seq 1 5`; do j[$i]=$(($i+1)) echo ${j[$i]} done -> 2 -> 3 -> 4 -> 5 -> 6 Here we first access the value of i by using While loopsYou can also make loops which execute as long as a certain condition is respected:
which is especially useful to make infinite loops by using Loop controlMuch like many other languages, Bash allows you to:
|