bash if -a vs -e option

前端 未结 3 1287
执笔经年
执笔经年 2020-12-08 02:09

Both about -a and -e options in Bash documentation is said:

-a file
    True if file exists.
-e file
    True if file exists. 
         


        
3条回答
  •  执念已碎
    2020-12-08 02:34

    The double bracket [[ exp ]] is a bash builtin. In bash -a and -e are the same, probably for some backwards compatibility.

    The single bracket [ exp ] is an alias for the external command "test". In "test", -a is a logical AND. Although [ nothing AND $STRING ] looks like it should be false, test has some syntax quirks, which is why I recommend using the bash builtin [[ exp ]], which tends to be more sane.

    Note: bash really does call /bin/[ when you use "[".

    $ [ $UNASIGNED_VAR == "bar" ]
    bash: [: ==: unary operator expected
    

    the error shows bash called [. An strace also shows "execve("/usr/bin/[", ..."

提交回复
热议问题