Idiomatic way to test if no positional params are given?

后端 未结 5 867
暖寄归人
暖寄归人 2021-01-13 10:03

What is the most idiomatic way in Bash to test if no positional parameters are given? There are so many ways to check this, I wonder if there is one preferred way.

S

5条回答
  •  情书的邮戳
    2021-01-13 10:40

    I prefer using the fact that if there are no positional parameters, there is also no first parameter:

    [[ -z $1 ]]
    test -z "$1"
    [ -z "$1" ]
    

    It's just a tiny bit lighter on the reader. Of course it only works when the assumption that the first parameter can't be an empty string is true.

提交回复
热议问题