How do you check if $* is empty? In other words, how to check if there were no arguments provided to a command?
3 Answers
To check if there were no arguments provided to the command, check value of $# variable then,
if [ $# -eq 0 ]; then
>&2 echo "No arguments provided"
exit 1
fi
If you want to use $*(not preferable) then,
if [ "$*" == "" ]; then
>&2 echo "No arguments provided"
exit 1
fi
Some explanation:
The second approach is not preferable because in positional parameter expansion * expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That means a string is constructed. So there is extra overhead.
On the other hand # expands to the number of positional parameters.
Example:
$ command param1 param2
Here,
Value of $# is 2 and value of $* is string "param1 param2" (without quotes), if IFS is unset. Because if IFS is unset, the parameters are separated by spaces
For more details man bash and read topic named Special Parameters
-
7Or
if ! (($#)); ..., orif (($# == 0)); ..., orif [ $# -eq 0 ]; ..., or! (($#)) && ..., or(($#)) || ...nicerobot– nicerobot2011-12-02 21:13:53 +00:00Commented Dec 2, 2011 at 21:13 -
9
[ $# -eq 0 ]is the most common form IME. There are edge case where"$#"can be empty: if there's a single argument which is empty, or if there are several empty arguments and$IFSis empty.Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2011-12-02 23:50:35 +00:00Commented Dec 2, 2011 at 23:50 -
1The
"$*"expression will also evaluate to""if only one""parameter was passed. But most of the time you will probably not care about anyway.manatwork– manatwork2011-12-03 13:15:53 +00:00Commented Dec 3, 2011 at 13:15 -
I believe it should be = instead of == ... currently I'm getting an unexpected operator error, and changing to = fixes the problem.well actually– well actually2011-12-05 18:02:56 +00:00Commented Dec 5, 2011 at 18:02
-
@Charlotte That means you are not using
bash. You are usingshto execute the script. AFAIK==is valid only inbash.Sachin Divekar– Sachin Divekar2011-12-05 19:05:29 +00:00Commented Dec 5, 2011 at 19:05
If you're only interested in bailing if a particular argument is missing, Parameter Substitution is great:
#!/bin/bash
# usage-message.sh
: ${1?"Usage: $0 ARGUMENT"}
# Script exits here if command-line parameter absent,
#+ with following error message.
# usage-message.sh: 1: Usage: usage-message.sh ARGUMENT
-
1what is the name of this technique using the
?in the bracket expansion${}? I cannot wrap myn head around the meaning and behaviour of it. I suppose?applies to th1in$1; but I'm left clueless, I'd gladly dig deeper this technique/syntax.Stephane Rolland– Stephane Rolland2021-05-05 13:41:54 +00:00Commented May 5, 2021 at 13:41 -
1aka parameter expansion: gnu.org/software/bash/manual/html_node/…jgreve– jgreve2021-08-09 20:44:07 +00:00Commented Aug 9, 2021 at 20:44
-
How about an optional parameter?CivFan– CivFan2022-09-16 19:39:40 +00:00Commented Sep 16, 2022 at 19:39
this is one of the ways you can know that you havent got any arguments
NO_ARGS=0
if [ $# -eq "$NO_ARGS" ]; then
{do something}
fi
-
9That seems to be a convoluted way of doing what the accepted answer wrote 5 years ago....2016-10-04 10:06:48 +00:00Commented Oct 4, 2016 at 10:06