5

In an attempt to stay consistent i have tried to use double brackets [[ ]] in all my if statements. I did however get into a problem when i was going to check the return value for a command i wanted to run. After testing several ways of creating an if statement i found that only without brackets could i execute a command.

The following does not work:

if [[ $command ]] ; then
    echo "something"
fi

if [[ $(command) ]] ; then
    echo "something"
fi

if [[ ${command} ]] ; then
    echo "something"
fi

and the code above makes the if loop true even when the command was not run. since the code above doesnt work with braces it doesnt work to use this either:

[[ $command ]] || echo "failed"

and it doesnt work in a subshell either.

The following works:

if $command ; then
    echo "something"
fi

if $(command) ; then
    echo "something"
fi

Why doesnt it work to place a command in an if loop with brackets, and why does the if loops above report true when it didnt even run the command ? I'm using bash version 4.1.9. Ive tried this many times and the if loops are just as simple as the ones i typed above, it just checks if a command was run successfully and exits if it wasnt.

3
  • did you try if [[ `$command`]] Feb 10, 2014 at 15:55
  • that doesnt work for me and the if loop also returns false. it also doesnt look good or seem like the recommended way to go. also i dont understand why it works without [[ ]] and not with.. isnt also the [[ ]] suppose to be an extension to bash so it should work
    – user2959896
    Feb 10, 2014 at 15:58
  • Double braces are a shortcut for calling test. man test to find out details. If you want to test a program for exit status, then use if someProgram; then echo "It worked!"; fi and that will work just fine and you don't need the double braces. Depending on what you're doing. Feb 10, 2014 at 16:01

3 Answers 3

6

The short answer is:

  • [ and [[ expect an expression.

  • if expects a command.

Saying:

[[ $(command) ]]

would essentially execute:

[[ -n <command_output> ]]

which may or may not be what you want. On the other hand, saying:

$command && echo something || echo other

would echo something or other based on the return code of the command (0 and non-zero respectively).

5
  • 1
    That said, if [[ $(command) ]] will work, if work means command writes to standard output.
    – kojiro
    Feb 10, 2014 at 16:00
  • Hmm, ok, but if [[ $(command) ]] doesnt work for me, im just executing it like a normal command and i the default is to write the output of the command to standard output isnt it?
    – user2959896
    Feb 10, 2014 at 16:01
  • There is no default behavior. Some commands write to standard output, some don't.
    – kojiro
    Feb 10, 2014 at 16:05
  • Ok, thanks for your very quick answer. I have been reading in the bash manual but i cant remember or see that what you wrote was explained there
    – user2959896
    Feb 10, 2014 at 16:17
  • @user2959896 You should see that in the manual. You should also be able to see some of it by saying help [[; help if.
    – devnull
    Feb 10, 2014 at 16:19
3

If you're just checking the return value of the command, drop the double brackets.

if $command
then
    echo "Command succeeded"
else
    echo "Command failed: $!"
fi

The double brackets are a test command. (Well, not really, but their a takeoff of the single square brackets that were an alias to the test command.) In early Bourne shell, you would see things like:

if test -z "$string"
then
    echo "This is an empty string"
fi

The square brackets were syntactic sugar:

if [ -z "$string" ]
then
    echo "This is an empty string"
fi

So, if you're not doing an actual test, you can eliminate the double or single square brackets.

If you're using square brackets, you should use the double ones and not the single ones because the double ones are a bit more forgiving and can do a bit more:

if [ -z $string ]    #  No quotes: This will actually fail if string is zero bytes!

if [[ -z $string ]]  #  This will work despite the lack of quotes
2

Double braces are a shortcut for test. In your examples, what's happening is that you're testing the shell variable $command for existence.

if [[ $PWD ]]; then
    echo PWD is set to a value
fi

if [[ $NOT_A_REAL_VAR ]]; then
    echo Nope, its not set
fi

In your second example, you're using command substitution to check that command output something on standard output.

if [[ $(echo hi) ]]; then
    echo "echo said hi'
fi

if [[ $(true) ]]; then #true is a program that just quits with successful exit status
    echo "This shouldn't execute"
fi

Your third example is the same as your first, pretty much. You use the curly braces if you want to group your variables. for example if you want to put an 's' after something.

WORD=Bike
echo "$WORDS" #won't work because "WORDS" isn't a variable
echo "${WORD}S" # will output "BikeS"

Then in your fifth example, you are running the program that is sitting inside command.

So, if you want to test some strings, use [[ ]] or [ ]. If you just want to test the exit status of a program, then don't use those, just use a bare if.

Check man test for details on the braces.

1
  • 1
    I realised you can test the program's exit status by having [[ $(false; echo -n $?) == 1 ]] or [[ $(true; echo -n $?) == 0 ]]. Neat! Dec 7, 2015 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.