[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash true/false builtings undocumented? "false" not working?

Hash: SHA1

On Fri, Aug 17, 2018 at 10:12:54AM -0400, Greg Wooledge wrote:
> On Fri, Aug 17, 2018 at 03:43:58PM +0200, tomas@tuxteam.de wrote:
> > The construction
> > 
> >   foo && echo "bar" || echo "baz"
> > 
> > does probably work, because echo's exit status is (always?) 0 (the bash
> > builtin's documentation mumbles something about "write error").
> echo can fail if stdout is closed, or if it's redirected to a file that
> can't be written to (because of a full disk, etc.).
> Sure, it may work 99.99% of the time, but it's still not safe.

This particular idiom is still fine, since if stdout gets closed,
nobody will notice the difference between both branches anyway [1].

I still maintain that this pattern is OK, since it's idiomatic and
highly readable. But of course, you gotta know what you're doing,
and in which ways it may break down. And blog pages like yours are
invaluable in that (although we may disagree (methodically( somewhat
in this point, I am thankful that you go to the length of writing
out those things in a generally accessible manner).

Shell only looks simple at first sight. It's a nice "little language",
full of surprises and treasures. But it has to be treated with
respect :-)


[1] https://en.wikipedia.org/wiki/If_a_tree_falls_in_a_forest
   somewhat (much) tongue-in-cheek :-P
- -- t
Version: GnuPG v1.4.12 (GNU/Linux)


Reply to: