Friday, April 18, 2008

Bash Tip of the Day! for loops

Today's tip covers Bash for loops. A simple operation that comes in handy with nearly EVERY shell script.


for i in $(seq 1 10)
do
echo ${i}
done


Note that enclosing variables in curly braces is not required but will never let you down. Take the following code, for example:


i=1
echo $i # works
echo $i5 # won't work!
echo ${i}5 # works!


Enclosing the variable in curly braces allows you to use the variable anywhere, without fear of Bash interpreting surrounding characters as part of the variable.

No comments: