help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] confused by command substitution...


From: Mike Gerwitz
Subject: Re: [Help-bash] confused by command substitution...
Date: Sat, 09 Feb 2019 10:18:44 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)

On Sat, Feb 09, 2019 at 06:01:53 -0500, Alfred M. Szmidt wrote:
> I'm not understanding the difference here, or why the last example
> works.  Anyone got a good explanation?

When the subshell is enclosed in a string, then its output is executed
as a command in this context.  Otherwise, the subshell is treated as the
command to `if' and its exit code is used.

> $ if "$(echo x)"; then echo foo; fi
> bash: x: command not found

$ if "x"; then echo foo; fi
bash: x: command not found

> $ if "$(echo x > /dev/null)"; then echo foo; fi
> bash: : command not found

$ if ""; then echo foo; fi
bash: : command not found

> $ if $(echo x); then echo foo; fi
> bash: x: command not found

$ if x; then echo foo; fi
bash: x: command not found

> $ if $(echo x > /dev/null); then echo foo; fi
> foo

$ if $(); then echo foo; fi
foo

So:

$ if "$(echo true)"; then echo foo; fi
foo

-- 
Mike Gerwitz
Free Software Hacker+Activist | GNU Maintainer & Volunteer
GPG: D6E9 B930 028A 6C38 F43B  2388 FEF6 3574 5E6F 6D05

Attachment: signature.asc
Description: PGP signature


reply via email to

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