Re:Shell programming with bash: by example, by counter-example
by zx2c4 on Monday January 13, 2014 @12:51PM
(#7052111)
Seconded for [[ and ]]. Also { and } (with semi colons) vs ( and ), for sub processes would be nice to add.
Re:Shell programming with bash: by example, by counter-example
by lsiebert on Tuesday January 14, 2014 @07:09AM
(#7056569)
I'd also note that your discussion of scope doesn't mention local variables are possible in bash, see: http://www.tldp.org/LDP/abs/html/localvar.html
Re:Shell programming with bash: by example, by counter-example
by barrkel on Tuesday January 14, 2014 @08:21AM
(#7056816)
I think it might be lacking content on idioms.
Things like 'while read -r line; do {stuff with $line}; done'
These idioms have issues for their most common uses (e.g. dealing with filenames with embedded newlines), but they do work 99% of the time.
Re:Shell programming with bash: by example, by counter-example
by unhammer on Friday January 17, 2014 @03:01AM
(#7074792)
They work 100 % of the time if you do it right. An example:
find . -type f -name foo -print0 | while read -rd ''; do
grep -l bar "${REPLY}"
done
find separates its output with the NUL byte when you do -print0, and -d '' means read NUL-separated input.Of course, in this case it would be better to use:
find . -type f -name foo -print0 | xargs -0 -I{} grep -l bar '{}'
but if you've got several things to do for each file, the first one makes sense (or you can put those several things in a script, and use xargs).
Re:Shell programming with bash: by example, by counter-example
by unhammer on Friday January 17, 2014 @02:50AM
(#7074763)
http://mywiki.wooledge.org/BashFAQ/031 is a great reference.
If your guide is for bash and not sh, then I would say don't teach them [, just do [[. It's safer, since it won't glob or split words, you don't need quoting, and you don't need to escape operators like < and >. Maybe it's can be seen as more difficult since it's a sublanguage (doesn't use the regular syntax rules), but so are (( and $(( which you've discussed at length, and which are typically less useful than [[ in bash.
I think I'll add it in a revision.
I also agree on bash being quirky. I tell my class that the gold standard is a bash script that works 99% of the time.