Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 57 down vote accepted

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

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

if [ "$*" == "" ]; then
    echo "No arguments provided"
    exit 1

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.


$ command param1 param2


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

share|improve this answer
Or if ! (($#)); ..., or if (($# == 0)); ..., or if [ $# -eq 0 ]; ..., or ! (($#)) && ..., or (($#)) || ... – nicerobot Dec 2 '11 at 21:13
[ $# -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. – Gilles Dec 2 '11 at 23:50
The "$*" expression will also evaluate to "" if only one "" parameter was passed. But most of the time you will probably not care about anyway. – manatwork Dec 3 '11 at 13:15
I believe it should be = instead of == ... currently I'm getting an unexpected operator error, and changing to = fixes the problem. – Charlotte Dec 5 '11 at 18:02
@Charlotte That means you are not using bash. You are using sh to execute the script. AFAIK == is valid only in bash. – Sachin Divekar Dec 5 '11 at 19:05

If you're only interested in bailing if a particular argument is missing, Parameter Substitution is great:

# 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|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.