Idiomatic way to test if no positional params are given?

后端 未结 5 818
暖寄归人
暖寄归人 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:35

    If you want it to be an error to have no positional parameters:

    : ${@?no positional parameters}
    

    will print "no positional parameters" to standard error (and exit a non-interactive shell) if $@ is unset.

    Otherwise, I'm not aware of any better options than one of the various methods of checking if $# is 0.

提交回复
热议问题