How to check if there are no parameters provided to a command?

188,332

Solution 1

To check if there were no arguments provided to the command, check value of $# variable then,

if [ $# -eq 0 ]; then
    echo "No arguments provided"
    exit 1
fi

If you want to use $*(not preferable) then,

if [ "$*" == "" ]; then
    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

Solution 2

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
Share:
188,332

Related videos on Youtube

well actually
Author by

well actually

Updated on September 18, 2022

Comments

  • well actually
    well actually almost 2 years

    How do you check if $* is empty? In other words, how to check if there were no arguments provided to a command?

  • nicerobot
    nicerobot over 12 years
    Or if ! (($#)); ..., or if (($# == 0)); ..., or if [ $# -eq 0 ]; ..., or ! (($#)) && ..., or (($#)) || ...
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 12 years
    [ $# -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 $IFS is empty.
  • manatwork
    manatwork over 12 years
    The "$*" expression will also evaluate to "" if only one "" parameter was passed. But most of the time you will probably not care about anyway.
  • well actually
    well actually over 12 years
    I believe it should be = instead of == ... currently I'm getting an unexpected operator error, and changing to = fixes the problem.
  • disco.dan.silver
    disco.dan.silver over 12 years
    @Charlotte That means you are not using bash. You are using sh to execute the script. AFAIK == is valid only in bash.
  • well actually
    well actually over 12 years
    ah, okay. good to know.
  • Jürgen Paul
    Jürgen Paul over 9 years
    what does $# even mean?
  • MichaelChirico
    MichaelChirico about 8 years
    @Michelle "# expands to the number of positional parameters." as stated in answer...
  • MichaelChirico
    MichaelChirico about 8 years
    If you'll indulge me, what is the inverse of this operation? -neq didn't work and I'm not doing anything if arguments are missing, so no sense creating that branch unnecessarily.
  • Jeff Schaller
    Jeff Schaller over 7 years
    That seems to be a convoluted way of doing what the accepted answer wrote 5 years ago....
  • Stephane Rolland
    Stephane Rolland about 3 years
    what 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 th 1 in $1 ; but I'm left clueless, I'd gladly dig deeper this technique/syntax.
  • jgreve
    jgreve almost 3 years