Does not work to execute command in double brackets in bash

后端 未结 3 384
不知归路
不知归路 2021-01-21 12:41

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 f

3条回答
  •  醉酒成梦
    2021-01-21 13:18

    The short answer is:

    • [ and [[ expect an expression.

    • if expects a command.

    Saying:

    [[ $(command) ]]
    

    would essentially execute:

    [[ -n  ]]
    

    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).

提交回复
热议问题