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

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

Asked By: well actually


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

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

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

Answered By: Sachin Divekar

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


: ${1?"Usage: $0 ARGUMENT"}
#  Script exits here if command-line parameter absent,
#+ with following error message.
# 1: Usage: ARGUMENT
Answered By: Pat

this is one of the ways you can know that you havent got any arguments

if [ $# -eq "$NO_ARGS" ]; then
    {do something}
Answered By: Joel Dsouza
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.