I'm having a discussion about the use of seq
for small loop in a Bash script (for example a loop with 10 iterations).
I say, for example, is better do this
for i in {1..10}
do
echo "Welcome $i times"
done
than use seq
for i in $(seq 10)
do
echo "Welcome $i times"
done
as written here: http://www.cyberciti.biz/faq/bash-for-loop/
In my opinion is useless for short loop use a not a built-in command. What about performance for example?
seq
comes handy when you do not know the range you are about to perform: you can say seq $a $b
, whereas you cannot say {$a..$b}
.
Also, {}
is available just for Bash, so in for example Shell you won't be able to use it.
That being said, if you are targeting bash then using {1..10}
is certainly going to be faster (and cheaper) than using seq
and that can matter. seq
is an external command, and that makes the execution slower.
From man bash
:
Brace Expansion
Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.
...
Brace expansion introduces a slight incompatibility with historical versions of sh. sh does not treat opening or closing braces specially when they appear as part of a word, and preserves them in the output. Bash removes braces from words as a consequence of brace expansion. For example, a word entered to sh as file{1,2} appears identically in the output. The same word is output as file1 file2 after expansion by bash. If strict compatibility with sh is desired, start bash with the +B option or disable brace expansion with the +B option to the set command.