4 ms·
Nitpick: although the linked page states that defining functions as compund commands is a `bash` feature, it is actually in the POSIX `sh` standard [1] so it sh
by HerrMonnezza 8y ago
Nitpick: although the linked page states that defining functions as
compund commands is a `bash` feature, it is actually in the POSIX `sh`
standard [1] so it should work with any POSIX-compliant shell.
[1]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_05 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3...
That said, I'm curious what are the nifty tricks that one can do by
using functions as "aliases" for compound commands -- I cannot see
much of a difference between `function sleep1 () { while true; do "$@";
sleep 1; done; }` and the similar definition without the curly braces.
Only once in my life I have had to use the `function foo () ( ... )`
syntax (i.e., using subshell instead of normal `{ ... }` grouping).
- schoen 8y agoI'm always amazed at how many Unix features I assume are GNUisms or bashisms that turn out to be in POSIX and be much more portable than I've expected!
- webreac 8y agoI'm always amazed at how scripts intented to use only POSIX features become buggy when dash is used instead of bash.
- yjftsjthsd-h 8y agoYeah, for anything that I want to be portable, I explicitly run it with bash, dash, mksh, and heirloom sh. I figure that if it works in all of those, then the result is either truly portable... or nobody will ever notice:) It also should really help that Debian, IIRC, actually sets /bin/sh to dash by default?
- frou_dh 8y agoFor those who've never looked at the POSIX docs, bookmark this: http://shellhaters.org http://shellhaters.org ...which is a great jumping off point to casually read a bit of them now and again.
- ben509 8y agoThe subshell syntax is great for: ( cd some_directory do_a_thing_here ) ... instead of all the pushd popd nonsense, and so much easier to follow. You can also do this: ( set -e bail_from_this if_anything_fails ) < redirect || echo 'Something went wrong here, carrying on...' And that won't set -e in your outer shell. You can also set -v to see what's being run just for those commands. As far as writing functions without braces, I don't. It doesn't do anything useful, it's magical and weird to bash n00bs, and when you have to add a command you'll wind up adding the braces then. The reason bourne shells have that is because the shell's raison d'etre is composing processes, so it makes sense that compound statements are simply processes themselves. (Maybe process is the wrong term here...) To expose that in the syntax, though, is just being overly clever, and it's more obvious now that people find it confusing.