[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Evaluations of backticks in if statements
From: |
Andy Chu |
Subject: |
Re: [Help-bash] Evaluations of backticks in if statements |
Date: |
Thu, 23 Feb 2017 08:57:07 -0800 |
On Wed, Feb 22, 2017 at 5:01 PM, Ryan Marples <address@hidden>
wrote:
> Hi,
>
> I’ve seen bash statements like the following: if `cmd` …
>
> This appears to run the body of the if when cmd exits successfully. But I
> don’t understand the mechanics of why. My understanding is that `cmd`
> (assuming in this case cmd prints nothing to either stdout nor stderr but
> exits successfully) should evaluate to an empty string, and then you’d be
> saying if “” … which should be false.
>
Shell doesn't work this way. The syntax is "if command", and
if `command`
is a particular case of that which is not special in any way (and as you
mention it's also not useful). There's no such thing as
if ""
where "" is a string/variable value -- the "" is interpreted a command name.
If you want to test if a value is empty, you can use
if test -n ""
or as a shortcut
if test ""
Or any of these:
[ -n "" ]
[ "" ]
[[ -n "" ]]
[[ "" ]]
is an alias for test and [[ is a special shell construct that behaves
somewhat like test / [.
Andy
- [Help-bash] Evaluations of backticks in if statements, Ryan Marples, 2017/02/23
- Re: [Help-bash] Evaluations of backticks in if statements, Greg Wooledge, 2017/02/23
- Re: [Help-bash] Evaluations of backticks in if statements,
Andy Chu <=
- Re: [Help-bash] Evaluations of backticks in if statements, Seth David Schoen, 2017/02/23
- Re: [Help-bash] Evaluations of backticks in if statements, Andy Chu, 2017/02/23
- Re: [Help-bash] Evaluations of backticks in if statements, Andy Chu, 2017/02/23
- Re: [Help-bash] Evaluations of backticks in if statements, Andy Chu, 2017/02/23
- Re: [Help-bash] Evaluations of backticks in if statements, Andy Chu, 2017/02/23