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: Eric Blake
Subject: Re: [Help-bash] Undocumented caret logical operator?
Date: Wed, 2 May 2018 13:03:07 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0

On 05/02/2018 12:51 PM, G. Branden Robinson wrote:
Can someone tell me what is going on here?  This operator is not
documented in SUSv4 nor in the Bash man page, at least not per my
full-text search.  The experiments show that it is not acting as an
"undocumented synonym for |" (bashref re: SVR4.2's Bourne shell),

Correct, because it is NOT an operator any more. (only super-old sh, not POSIX compatible, treated it as an operator)


and
the behavior does not change from a script or with set +o history, so
it's not a null version of the ^foo^bar history operator.  We can also
rule out arithmetic expansion (bitwise xor) and glob expansion
(character class inversion), as we're in the wrong lexical context.

dash and ksh93 behave identically.  What am I missing?  What use cases
does it serve?

echo -n expect TRUE:
true ^ true && echo TRUE || echo FALSE

This executes the command 'true ^ true'; then, if that command had status 0, it executes 'echo TRUE'; then, if that && group had status 1, it executes 'echo FALSE'.

The command 'true ^ true' has status 0 ('true' always ignores all of its arguments), the same way that 'true gibberish' has status 0.

If you want to see a bit more into what's going on:

$ (set -x; true ^ true && echo TRUE || echo FALSE)
+ true '^' true
+ echo TRUE
TRUE

echo -n expect OUTPUT and no diagnostic:
false ^ nonexistent-command | echo OUTPUT

And this says to run the command 'false ^ nonexistent-command' (as with true, false ignores argv[1]=="^" and argv[2]=="nonexistent-command" to produce no output), and pipes the result to the input of 'echo OUTPUT' (which ignores input and produces its output unconditionally).

It does not attempt to run nonexistent-command, because ^ is not an operator.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org



reply via email to

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