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.
Some ways are:
((! $# )) # check if $# is 'not true'
(($# == 0)) # $# is 0
[[ ! $@ ]] # $@ is unset or null
For me, the classical way is:
Use Sensible Semantics
The key is readability and the intent of your code. Unless you have a good reason to do otherwise, you probably just want to determine the length of the parameter list.
However, you can certainly use any parameter expansion or comparison operator that expresses the intent of your code. There's no right way to do it, but you certainly may wish to consider whether the semantics of your test are portable.
Food for Thought
It isn't the conditional expression that's intrinsically important. What's important is why you want to know. For example:
In this case, the length of the parameter list is never checked directly. The first parameter is simply assigned (even though it may be unset), and then the shell throws an error and exits when you try to shift the parameter list. This code says "I just expect the parameters to be there; I shouldn't have to check for them explicitly."
The point here is that you need to determine what your code is trying to express, and match the semantics of your tests and conditionals to express that as clearly as you can. There really is no orthogonal answer.
Here's a most logical way:
It is based on a single rule:
Well then,
The rest is obvious: $@ is the special parameter that expands to a list of all positional parameters.
Test: (It will work even if you throw a couple of empty strings ("" "" "") at it.)
If you want it to be an error to have 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.I prefer using the fact that if there are no positional parameters, there is also no first parameter:
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.