Little Girl's Mostly Linux Blog

Arguments

Command Line Arguments

Command Line Options

This page was last updated on March 06, 2014.
This page should really be named Command Line Options. Apparently I’ve been wrong about command line arguments for several years! It was only recently that I discovered that in Unix-like operating systems, what I’ve been calling an argument is actually an option, and that options can accept or require arguments. This provides three basic ways to run commands (programs or scripts):

  • Run a command without any options or arguments.
  • Run a command with one or more options without any arguments. An option is a choice that you can make with a command. You can use an option by typing a command followed by a space followed by a dash followed by a specific (or any) character or characters.
  • Run a command with one or more options and arguments. An argument is a string of one or more characters that are used by the option. You can use an argument by typing a command followed by a space followed by a dash followed by a specific (or any) character or characters followed by a space followed by a dash followed by a string of one or more characters, preferably enclosed in quotes to ensure the script will behave as intended.
For example, if you have a command called mycommand and it accepts options and arguments, you can run the command three different ways:

  • Type bash mycommand to run the command without an option.
  • Type bash mycommand -a to run the command with the a option.
  • Type bash mycommand -a “example” or bash mycommand -a example to run the command with the a option and provide the example argument to the a option.
When writing BASH scripts, you can use various types of statements to work with command line options and arguments. There are subtle differences in how they handle the options and arguments, so the method you use will depend on your individual needs or preferences. I suggest trying them all and deciding which you like best. Below are three different ways of running commands. Each example shows side-by-side comparisons of if, case, and getopts statements being used to check for command line options and/or arguments and react appropriately to what they find.

Example 1 – if

#!/bin/bash

#######################################################
# Run this script with:                               #
#   - no options to display one message.              #
#   - one or more options to display another message. #
#######################################################

################
# IF STATEMENT #
################
if [ $# = '0' ]; then
	echo "You chose no option.";
else
	echo "You chose any option(s).";
fi;

Example 1 – case

#!/bin/bash

#######################################################
# Run this script with:                               #
#   - no options to display one message.              #
#   - one or more options to display another message. #
#######################################################

##################
# CASE STATEMENT #
##################
case $1 in
	'')
		echo "You chose no option.";
		;;
	*)
		echo "You chose any option(s).";
		;;
esac;

Example 1 – getopts

#!/bin/bash

#######################################################
# Run this script with:                               #
#   - no options to display one message.              #
#   - one or more options to display another message. #
#######################################################

#####################
# GETOPTS STATEMENT #
#####################
while getopts "::" opt;
	do
		case $opt in
			\?)
				echo "You chose any option(s).";
				exit 1;
				;;
		esac;
	done;
echo "You chose no option.";

Example 2 – if

#!/bin/bash

#############################################
# Run this script with:                     #
#   - no options to use VARIABLE1.          #
#   - one or more options to use VARIABLE2. #
#############################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option.";
	}
VARIABLE2() {
	echo "You chose any option(s).";
	}

################
# IF STATEMENT #
################
if [ $# = '0' ]; then
	VARIABLE1;
else
	VARIABLE2;
fi;

Example 2 – case

#!/bin/bash

#############################################
# Run this script with:                     #
#   - no options to use VARIABLE1.          #
#   - one or more options to use VARIABLE2. #
#############################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option.";
	}
VARIABLE2() {
	echo "You chose any option(s).";
	}

##################
# CASE STATEMENT #
##################
case $1 in
	'')
		VARIABLE1;
		;;
	*)
		VARIABLE2;
		;;
esac;

Example 2 – getopts

#!/bin/bash

#############################################
# Run this script with:                     #
#   - no options to use VARIABLE1.          #
#   - one or more options to use VARIABLE2. #
#############################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option.";
	}
VARIABLE2() {
	echo "You chose any option(s).";
	}

#####################
# GETOPTS STATEMENT #
#####################
while getopts "::" opt;
	do
		case $opt in
			\?)
				VARIABLE2;
				exit 1;
				;;
		esac;
	done;
VARIABLE1;

Example 3 – if

#!/bin/bash

############################################################################
# Run this script with:                                                    #
#   - no options to display the specified message.                         #
#   - the -a option to display the specified message.                      #
#   - the -b option to display the specified message.                      #
#   - the -c option to display the specified message.                      #
#   - invalid input or an unknown option to display the specified message. #
#   - multiple options to display the specified message.                   #
############################################################################

################
# IF STATEMENT #
################
if [ $# = '0' ]; then
	echo "You chose no option.";
elif [ $# = "1" ]; then
	if [ "$1" = "-a" ]; then
		echo "You chose the -a option.";
	elif [ "$1" = "-b" ]; then
		echo "You chose the -b option.";
	elif [ "$1" = "-c" ]; then
		echo "You chose the -c option.";
	else
		echo "Invalid input or you chose an unknown option.";
	fi;
else
	echo "You chose more than one option.";
fi;

Example 3 – case

#!/bin/bash

############################################################################
# Run this script with:                                                    #
#   - no options to display the specified message.                         #
#   - the -a option to display the specified message.                      #
#   - the -b option to display the specified message.                      #
#   - the -c option to display the specified message.                      #
#   - invalid input or an unknown option to display the specified message. #
#   - multiple options to react only to the first option.                  #
############################################################################

##################
# CASE STATEMENT #
##################
case $1 in
	'')
		echo "You chose no option.";
		;;
	-a)
		echo "You chose the -a option.";
		;;
	-b)
		echo "You chose the -b option.";
		;;
	-c)
		echo "You chose the -c option.";
		;;
	-*)
		echo "You chose an unknown option.";
		;;
	*)
		echo "Invalid input.";
		;;
esac

Example 3 – getopts

#!/bin/bash

#####################################################################
# Run this script with:                                             #
#   - no options or invalid input to display the specified message. #
#   - the -a option to display the specified message.               #
#   - the -b option to display the specified message.               #
#   - the -c option to display the specified message.               #
#   - an unknown option to display the specified message.           #
#   - multiple options to display the specified message.            #
#####################################################################

#####################
# GETOPTS STATEMENT #
#####################
while getopts ":abc" opt;
	do
		case $opt in
			a)
				echo "You chose the -a option.";
				exit 1;
				;;
			b)
				echo "You chose the -b option.";
				exit 1;
				;;
			c)
				echo "You chose the -c option.";
				exit 1;
				;;
			\?)
				echo "You chose an unknown option.";
				exit 1;
				;;
		esac;
	done;
echo "You chose no option or provided invalid input.";

Example 4 – if

#!/bin/bash

############################################################
# Run this script with:                                    #
#   - no options to use VARIABLE1.                         #
#   - the -a option to use VARIABLE2.                      #
#   - the -b option to use VARIABLE3.                      #
#   - the -c option to use VARIABLE4.                      #
#   - invalid input or an unknown option to use VARIABLE5. #
#   - multiple options to use VARIABLE6.                   #
############################################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option.";
	}
VARIABLE2() {
	echo "You chose the -a option.";
	}
VARIABLE3() {
	echo "You chose the -b option.";
	}
VARIABLE4() {
	echo "You chose the -c option.";
	}
VARIABLE5() {
	echo "Invalid input or you chose an unknown option.";
	}
VARIABLE6() {
	echo "You chose more than one option.";
	}

################
# IF STATEMENT #
################
if [ $# = '0' ]; then
	VARIABLE1;
elif [ $# = "1" ]; then
	if [ "$1" = "-a" ]; then
		VARIABLE2;
	elif [ "$1" = "-b" ]; then
		VARIABLE3;
	elif [ "$1" = "-c" ]; then
		VARIABLE4;
	else
		VARIABLE5;
	fi;
else
	VARIABLE6;
fi;

Example 4 – case

#!/bin/bash

###########################################################
# Run this script with:                                   #
#   - no options to use VARIABLE1.                        #
#   - the -a option to use VARIABLE2.                     #
#   - the -b option to use VARIABLE3.                     #
#   - the -c option to use VARIABLE4.                     #
#   - an unknown option to use VARIABLE5.                 #
#   - invalid input to use VARIABLE6.                     #
#   - multiple options to react only to the first option. #
###########################################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option.";
	}
VARIABLE2() {
	echo "You chose the -a option.";
	}
VARIABLE3() {
	echo "You chose the -b option.";
	}
VARIABLE4() {
	echo "You chose the -c option.";
	}
VARIABLE5() {
	echo "You chose an unknown option.";
	}
VARIABLE6() {
	echo "Invalid input.";
	}

##################
# CASE STATEMENT #
##################
case $1 in
	'')
		VARIABLE1;
		;;
	-a)
		VARIABLE2;
		;;
	-b)
		VARIABLE3;
		;;
	-c)
		VARIABLE4;
		;;
	-*)
		VARIABLE5;
		;;
	*)
		VARIABLE6;
		;;
esac;

Example 4 – getopts

#!/bin/bash

###########################################################
# Run this script with:                                   #
#   - no options or invalid input to use VARIABLE1.       #
#   - the -a option to use VARIABLE2.                     #
#   - the -b option to use VARIABLE3.                     #
#   - the -c option to use VARIABLE4.                     #
#   - an unknown option to use VARIABLE5.                 #
#   - multiple options to react only to the first option. #
###########################################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option or entered invalid input.";
	exit 1;
	}
VARIABLE2() {
	echo "You chose the -a option.";
	exit 1;
	}
VARIABLE3() {
	echo "You chose the -b option.";
	exit 1;
	}
VARIABLE4() {
	echo "You chose the -c option.";
	exit 1;
	}
VARIABLE5() {
	echo "You chose an unknown option.";
	exit 1;
	}
 
while getopts ":abc" opt;
	do
		case $opt in
			a)
				VARIABLE2;
				;;
			b)
				VARIABLE3;
				;;
			c)
				VARIABLE4;
				;;
			\?)
				VARIABLE5;
				;;
		esac;
	done;
VARIABLE1;

Example 5 – if

#!/bin/bash

############################################################################
# Run this script with:                                                    #
#   - no options to display the specified message.                         #
#   - the -a or --aaa option to display the specified message.             #
#   - the -b or --bbb option to display the specified message.             #
#   - the -c or --ccc option to display the specified message.             #
#   - invalid input or an unknown option to display the specified message. #
#   - multiple options to display the specified message.                   #
############################################################################

################
# IF STATEMENT #
################
if [ $# = '0' ]; then
	echo "You chose no option.";
elif [ $# = "1" ]; then
	if [ "$1" = "-a" -o "$1" = "--aaa" ]; then
		echo "You chose the -a or --aaa option.";
	elif [ "$1" = "-b" -o "$1" = "--bbb" ]; then
		echo "You chose the -b or --bbb option.";
	elif [ "$1" = "-c" -o "$1" = "--ccc" ]; then
		echo "You chose the -c or --ccc option.";
	else
		echo "Invalid input or you chose an unknown option.";
	fi;
else
	echo "You chose more than one option.";
fi;

Example 5 – case

#!/bin/bash

################################################################
# Run this script with:                                        #
#   - no options to display the specified message.             #
#   - the -a or --aaa option to display the specified message. #
#   - the -b or --bbb option to display the specified message. #
#   - the -c or --ccc option to display the specified message. #
#   - an unknown option to display the specified message.      #
#   - invalid input to display the specified message.          #
#   - multiple options to react only to the first option.      #
################################################################

##################
# CASE STATEMENT #
##################
case $1 in
	'')
		echo "You chose no option.";
		;;
	-a|--aaa)
		echo "You chose the -a or --aaa option.";
		;;
	-b|--bbb)
		echo "You chose the -b or --bbb option.";
		;;
	-c|--ccc)
		echo "You chose the -c or --ccc option.";
		;;
	-*)
		echo "You chose an unknown option.";
		;;
	*)
		echo "Invalid input.";
		;;
esac;

Example 5 – getopts

#!/bin/bash

#####################################################################
# Run this script with:                                             #
#   - no options or invalid input to display the specified message. #
#   - the -a or --aaa option to display the specified message.      #
#   - the -b or --bbb option to display the specified message.      #
#   - the -c or --ccc option to display the specified message.      #
#   - an unknown option to display the specified message.           #
#   - multiple options to react only to the first option.           #
#####################################################################
 
while getopts ":abc" opt;
	do
		case $opt in
			a|aaa)
				echo "You chose the -a or -aaa option.";
				exit 1;
				;;
			b|bbb)
				echo "You chose the -b or -bbb option.";
				exit 1;
				;;
			c|ccc)
				echo "You chose the -c or -ccc option.";
				exit 1;
				;;
			\?)
				echo "You chose -$OPTARG which is an unknown option.";
				exit 1;
				;;
		esac;
	done;
echo "You chose no option or entered invalid input.";

Example 6 – if

#!/bin/bash

############################################################
# Run this script with:                                    #
#   - no options to use VARIABLE1.                         #
#   - the -a or --aaa option to use VARIABLE2.             #
#   - the -b or --bbb option to use VARIABLE3.             #
#   - the -c or --ccc option to use VARIABLE4.             #
#   - invalid input or an unknown option to use VARIABLE5. #
#   - multiple options to use VARIABLE6.                   #
############################################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option.";
	}
VARIABLE2() {
	echo "You chose the -a or --aaa option.";
	}
VARIABLE3() {
	echo "You chose the -b or --bbb option.";
	}
VARIABLE4() {
	echo "You chose the -c or --ccc option.";
	}
VARIABLE5() {
	echo "Invalid input or you chose an unknown option.";
	}
VARIABLE6() {
	echo "You chose more than one option.";
	}

################
# IF STATEMENT #
################
if [ $# = '0' ]; then
	VARIABLE1;
elif [ $# = "1" ]; then
	if [ "$1" = "-a" ]; then
		VARIABLE2;
	elif [ "$1" = "--aaa" ]; then
		VARIABLE2;
	elif [ "$1" = "-b" ]; then
		VARIABLE3;
	elif [ "$1" = "--bbb" ]; then
		VARIABLE3;
	elif [ "$1" = "-c" ]; then
		VARIABLE4;
	elif [ "$1" = "--ccc" ]; then
		VARIABLE4;
	else
		VARIABLE5;
	fi;
else
	VARIABLE6;
fi;

Example 6 – case

#!/bin/bash

###########################################################
# Run this script with:                                   #
#   - no options to use VARIABLE1.                        #
#   - the -a or --aaa option to use VARIABLE2.            #
#   - the -b or --bbb option to use VARIABLE3.            #
#   - the -c or --ccc option to use VARIABLE4.            #
#   - an unknown option to use VARIABLE5.                 #
#   - invalid input to use VARIABLE6.                     #
#   - multiple options to react only to the first option. #
###########################################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option.";
	}
VARIABLE2() {
	echo "You chose the -a or --aaa option.";
	}
VARIABLE3() {
	echo "You chose the -b or --bbb option.";
	}
VARIABLE4() {
	echo "You chose the -c or --ccc option.";
	}
VARIABLE5() {
	echo "You chose an unknown option.";
	}
VARIABLE6() {
	echo "Invalid input.";
	}

##################
# CASE STATEMENT #
##################
case $1 in
	'')
		VARIABLE1;
		;;
	-a|--aaa)
		VARIABLE2;
		;;
	-b|--bbb)
		VARIABLE3;
		;;
	-c|--ccc)
		VARIABLE4;
		;;
	-*)
		VARIABLE5;
		;;
	*)
		VARIABLE6;
		;;
esac;

Example 6 – getopts

#!/bin/bash

###########################################################
# Run this script with:                                   #
#   - no options or invalid input to use VARIABLE1.       #
#   - the -a or --aaa option to use VARIABLE2.            #
#   - the -b or --bbb option to use VARIABLE 3.           #
#   - the -c or --ccc option to use VARIABLE4.            #
#   - an unknown option to use VARIABLE5.                 #
#   - multiple options to react only to the first option. #
###########################################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option or entered invalid input.";
	exit 1;
	}
VARIABLE2() {
	echo "You chose the -a or --aaa option.";
	exit 1;
	}
VARIABLE3() {
	echo "You chose the -b or --bbb option.";
	exit 1;
	}
VARIABLE4() {
	echo "You chose the -c or --ccc option.";
	exit 1;
	}
VARIABLE5() {
	echo "You chose -$OPTARG which is an unknown option.";
	exit 1;
	}
 
while getopts ":abc" opt;
	do
		case $opt in
			a|aaa)
				VARIABLE2;
				;;
			b|bbb)
				VARIABLE3;
				;;
			c|ccc)
				VARIABLE4;
				;;
			\?)
				VARIABLE5;
				;;
		esac;
	done;
VARIABLE1;

Example 7 – if

TO BE COMPLETED...

Example 7 – case

TO BE COMPLETED...

Example 7 – getopts

#!/bin/bash

#################################################################################
# Run this script with:                                                         #
#   - no options or invalid input to display the specified message.             #
#   - the -a option to require an argument and display the specified message.   #
#   - the -b option to display the specified message.                           #
#   - the -c option to display the specified message.                           #
#   - an option without the required argument to display the specified message. #
#   - an unknown option to display the specified message.                       #
#   - multiple options to react only to the first option.                       #
#################################################################################

while getopts ":a:bc" opt;
	do
		case $opt in
			a)
				echo "You chose the -a option with the $OPTARG argument.";
				exit 1;
				;;
			b)
				echo "You chose the -b option.";
				exit 1;
				;;
			c)
				echo "You chose the -c option.";
				exit 1;
				;;
			\?)
				echo "-$OPTARG is not a valid option.";
				exit 1;
				;;
			:)
				echo "Option -$OPTARG requires an argument.";
				exit 1;
				;;
		esac;
	done;
echo "You chose no option or entered invalid input.";

Example 8 – if

TO BE COMPLETED...

Example 8 – case

TO BE COMPLETED...

Example 8 – getopts

#!/bin/bash

#################################################################
# Run this script with:                                         #
#   - no options or invalid input to use VARIABLE1.             #
#   - the -a option to require an argument and use VARIABLE2.   #
#   - the -b option to use VARIABLE3.                           #
#   - the -c option to use VARIABLE4.                           #
#   - an option without the required argument to use VARIABLE5. #
#   - an unknown option to use VARIABLE6.                       #
#   - multiple options to react only to the first option.       #
#################################################################

#############
# VARIABLES #
#############
VARIABLE1() {
	echo "You chose no option or entered invalid input.";
	exit 1;
	}
VARIABLE2() {
	echo "You chose the -a option with the $OPTARG argument.";
	exit 1;
	}
VARIABLE3() {
	echo "You chose the -b option.";
	exit 1;
	}
VARIABLE4() {
	echo "You chose the -c option.";
	exit 1;
	}
VARIABLE5() {
	echo "Option -$OPTARG requires an argument.";
	exit 1;
	}
VARIABLE6() {
	echo "-$OPTARG is not a valid option.";
	exit 1;
	}

while getopts ":a:bc" opt;
	do
		case $opt in
			a)
				VARIABLE2;
				;;
			b)
				VARIABLE3;
				;;
			c)
				VARIABLE4;
				;;
			:)
				VARIABLE5;
				;;
			\?)
				VARIABLE6;
				;;
		esac;
	done;
VARIABLE1;

See also:

Obligatory Happy Ending

And they all lived happily ever after. The end.

Back to top

Advertisements

4 Comments »

  1. I think this is bad practice and breaks the syntax. There is already getopt; see
    http://wiki.bash-hackers.org/howto/getopts_tutorial#using_ithttp://wiki.bash-hackers.org/howto/getopts_tutorial#using_it

    Comment by Baronsed — June 10, 2013 @ 7:09 pm

    • Thanks – I’ll look into it and add it to this page. :)

      Comment by mostlylinux — June 12, 2013 @ 4:03 pm

  2. Why have I been censored ? I think it is pertinent.
    Use getopt, it is way safer. http://wiki.bash-hackers.org/howto/getopts_tutorial

    Comment by Baronsed — June 12, 2013 @ 3:34 pm

    • I’m sorry it took so long to get these comments approved. I just moved, so I’m busy with getting settled in. I promise I’ll get to it and get it posted. I’ve already begun playing around with it yesterday. I just don’t have it written up yet in my usual step by step commented way. :)

      Comment by mostlylinux — June 12, 2013 @ 4:07 pm


RSS feed for comments on this post. TrackBack URI

Comment:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: