help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Undocumented caret logical operator?


From: Greg Wooledge
Subject: Re: [Help-bash] Undocumented caret logical operator?
Date: Wed, 2 May 2018 14:45:42 -0400
User-agent: NeoMutt/20170113 (1.7.2)

On Wed, May 02, 2018 at 01:38:15PM -0500, Eric Blake wrote:
> # check that we have something mildly sane as our shell, or try to find
> something better
> if false ^ printf "%s: WARNING: ancient shell, hunting for a more modern
> one... " "$0"
> then
> 
> Yes, on all modern systems (and any POSIXy shell), this executes the single
> command 'false' with ignored arguments, and skips the 'then' clause.
> Meanwhile, on old Solaris systems, where /bin/sh is Bourne shell rather than
> POSIX sh and has a ^ operator, this is parsed the same as 'false | printf
> ...', which executes the printf command (which may or may not have been a
> builtin in that old shell, and may or may not be on your PATH); it is HOPING
> that the exit status of printf will be 0, and that the WARNING message was
> printed, and that the 'then' clause then gets a chance to try re-execing
> under a saner shell.
> 
> But since printf is a relatively modern shell invention, I'm seriously
> wondering if a system old enough to have /bin/sh that understands ^ as a
> pipe operator would also lack printf, and thus break this snippet.

It also breaks if printf can't write to stdout for any reason, e.g. if
some fool closed file descriptor 1.  (I've seen people do this, because
they think >&- is a "shorter" or "more elegant" form of >/dev/null --
it may be shorter, but it's not equivalent!)  Or if stdout has been
redirected to a file on a file system that has run out of space, etc.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]