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: Greg Wooledge
Subject: Re: [Help-bash] confused by command substitution...
Date: Mon, 11 Feb 2019 08:49:35 -0500
User-agent: NeoMutt/20170113 (1.7.2)

On Sat, Feb 09, 2019 at 06:01:53AM -0500, Alfred M. Szmidt wrote:
> I'm not understanding the difference here, or why the last example
> works.  Anyone got a good explanation?
> 
> $ if "$(echo x)"; then echo foo; fi
> bash: x: command not found
> $ if "$(echo x > /dev/null)"; then echo foo; fi
> bash: : command not found
> $ if $(echo x); then echo foo; fi
> bash: x: command not found
> $ if $(echo x > /dev/null); then echo foo; fi
> foo

They're all 100% wrong.  If one of them happens not to throw a syntax
error, that's just bash's way of lulling newbies into complacency so
that their scripts blow up LATER instead of IMMEDIATELY.  Obviously a
script that APPEARS to work has a better chance of going into production
in a broken state.  This maximizes the chances of chaos and destruction.
Bash craves these things.  The suffering of fools is its lifeblood.

The "if" keyword is used as follows:

if COMMAND LIST ONE
then
  COMMAND LIST TWO
[ else
  COMMAND LIST THREE ]
fi

COMMAND LIST ONE is a list of commands.  These commands are executed,
just as if they were a little shell script.

After that list of commands is finished executing, bash looks at the
current value of $?.  If that value is 0, it executes COMMAND LIST TWO.
Otherwise, it executes COMMAND LIST THREE.  (There are also some
historical shenanigans involving overwriting the value of $? if it's
nonzero and there's no "else", but you didn't ask about this part.)

Now, since you didn't bother to tell us what the hell you are trying
to do, we cannot help you do it.  We don't know why you were trying
to execute the output of "echo x" as COMMAND LIST ONE in an if statement.
There is no POSSIBLE way we could know this.  It's so nonsensical that
we cannot guess what you intended, and therefore we can't help you at all.

If you actually want help doing something, tell us what it is.



reply via email to

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