Breaking Bad Habits - Don't Use seq in Your Shell Scripts
Like most, I learned shell scripting by following examples. Well, unfortunately, most of the samples I learned from used the 'seq' binary to execute a simple for loop like so:
for i in `seq 1 10`; do echo $i done
I discovered why this is bad today - not all Unixes (Solaris and Darwin included) come with it. Not to mention we're forking a process where we don't need it. On bash, use the built-in brace expansion instead:
for i in {1..10}; do
echo $i
done
For ksh and other shells, instead of using a for, use a while loop with an incrementing counter if the integers are too numerous to list in the loop header itself.

Comments
I agree with what you're saying...
but I really think the best way to write shell scripts is to use what you know, especially for the many people who have a relatively heterogeneous environment.
I do admit, I used to have an if clause at the top of a lot of mine that would test for OS X, and run gnu-date if appropriate, but I've moved to an entirely Linux-based infrastructure (thank God), and I suspect a lot of people are in a similar boat, although I don't have any hard facts to back that up.
I still use seq a lot, but the bash built-in stuff is very easy as well. I suppose it just depends on how many iterations you're going to do of the command. A few, and either will work. A few thousand, and you want to use the less resource-intensive options.
Convenience versus Portability
We're kinda the opposite, here. We have an 80/20 split of Solaris and Linux. Not only do we have to worry about included binaries, but there are some older Solaris 9 servers that have bash 2.05 which lacks a lot of the features present in bash 3.x. We also have a lot of ksh scripts laying around to babysit.
Like you said, if it's a quick hack, then just get it done as fast as possible. However, if it's going to be run via cron or something that will be around for awhile, we've found that taking the time to make it easily moved between *nixes usually pays dividends.
Of course, if my shell script won't fit on one screen, then it's usually time to write the script in Perl (but that's a topic for another post :-) )
Thanks for the comments!
if you're gonna take the time
if you're gonna take the time to write it in perl, write it in python or ruby. it's so much more legible and maintainable.
python comes on every modern system, and ruby is easily gotten. ruby is one of those things I make sure is there when I am on a new system, just like vim-full.
0 padding
I know a few people that use seq for 0 padding. You can do that with your example and printf.
for i in {1..10}; do printf '%02s' $i; done
time
using time and with a large number:
for:
real 0m43.669s
user 0m18.169s
sys 0m3.364s
seq:
real 0m53.787s
user 0m22.109s
sys 0m5.708s
so 10 seconds is a lot of time, i am changing to for too
Post new comment