3 ms·
Why take a perfectly readable if-statement and turn it into something, 99.9% of people would need to lookup. Concise != better. You can make it one line with:
by archargelod 2mo ago
Why take a perfectly readable if-statement and turn it into something, 99.9% of people would need to lookup. Concise != better. You can make it one line with:
[ -z "$1" ] && { echo "missing argument, aborting." 1>&2; exit 1 }
- tzot 2mo agoI believe you missed a semicolon after `exit 1` and before `}`. `}` closes the opening `{` only at the start of a command (POSIX rules; don't remember now if bash recognizes it when it's just a command argument).
- archargelod 2mo agoI'm just too used to how parsing works in Zsh. For bash and sh, there is indeed should be a semicolon.
- xorcist 2mo agoIn this specific case, echo can't fail to there's no need for any of that: [ -z "$1" ] && echo "fail" >&2 && exit 1
- layer8 2mo agoEcho will fail here when stderr is closed. For example when the caller of the script pipes stderr to another command and that command exits.
- refp 2mo agoThis question seems to pop up all over the place, so I took some time to answer it here: https://refp.se/articles/your-shell-and-the-magic-colon#why-would-i-do-any-of-these-when-it-hurts-readability https://refp.se/articles/your-shell-and-the-magic-colon#why-...
- notnmeyer 2mo agothe people writing it are doing it for themselves probably. i wouldn’t expect it to survive a code review. the necessity of using esoteric bash features or syntax is a pretty good smell that you should be using something else imo. different strokes of course.
- pimlottc 2mo agoThere’s a good chance that the people writing it will probably forget what it does in six months too :P