4 ms·
I was on the fence about adding [[ ]]. 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
by mattmight 13y ago
I was on the fence about adding [[ ]].
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.
- zx2c4 13y agoSeconded for [[ and ]]. Also { and } (with semi colons) vs ( and ), for sub processes would be nice to add.
- lsiebert 13y agoI'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 http://www.tldp.org/LDP/abs/html/localvar.html
- barrkel 13y agoI 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.
- unhammer 13y agoThey 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).
- unhammer 13y agohttp://mywiki.wooledge.org/BashFAQ/031 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.