4 ms·
Mentioned in the artice is: cat ~/.profile && echo This is your profile || echo Failed to read profile I think these are commonly called short-circuits. Using
by _joel 6y ago
Mentioned in the artice is:
cat ~/.profile && echo This is your profile || echo Failed to read profile
I think these are commonly called short-circuits. Using these with brackets too, in order to gain greater control or more complex comparisions is super useful if not aware of it.
A simple example would be something like:
# (foo && bar) && (bish || bash) && echo 0 || (echo "not bosh" && exit 1)
I'm no bash expert, just a sysadmin with a little dangerous knowledge, but constructing things that way just feels more natural (to me, at least, it's probably 'teaching your grandmother to suck eggs' to a proper bash hacker).
- mr-wendel 6y agoYou can group with curley braces too. This can be especially useful for input/output redirection over a chunk of commands. Be careful with using parenthesis -- this means you're now in a subshell! That "exit 1" will NOT terminate the entire script, but instead set "$?" to 1 for the next command. Additionally, any variable or environmental changes will be lost (which may be desirable) once you're outside of that block.
- _joel 6y agoThanks for the tip! I knew what I was doing was probably not right, but it worked when I needed it in the past (probably because of setting pipefail). It's very much welcome to know how to do it better :)
- cb321 6y agoCurly braces will fail in `dash` which may be what your /bin/sh is (or could become).
- anderskaseorg 6y agoCurly braces are POSIX and work fine in dash. (Perhaps when testing you forgot these restrictions, which are the same as in bash: there must be whitespace after {, and a newline or semicolon before }.) https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04 https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V...
- cb321 6y agoYou are right. Apologies. Seemingly from this [1], quoting such braces was initially recommended. [1] https://www.cs.ait.ac.th/~on/O/oreilly/unix/ksh/appa_02.htm https://www.cs.ait.ac.th/~on/O/oreilly/unix/ksh/appa_02.htm