Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 25, 2025
As Linux users, we frequently use various command-line utilities and scripts. One of the common tasks while developing a script is to parse command-line options. These options can be short or long.
In this tutorial, we’ll use bash‘s getopts function and the getopt utility for parsing the command-line options.
In Bash, we can use both short and long command-line options. The short option starts with the single hyphen (-) character followed by a single alphanumeric character, whereas the long option starts with the double hyphen (–) characters followed by multiple characters.
We can instruct the ls command to display hidden files:
$ ls -a
. .. hello.txt
$ ls --all
. .. hello.txt
In the above example:
To parse short command-line options, we can use bash‘s built-in function getopts. It parses positional parameters as options.
The syntax of the getopts function is:
getopts optstring opt [arg ...]
In the above function:
In the following sections, these concepts will become clear as we see them in action.
Let’s create a simple shell script to see its usage:
#!/bin/bash
while getopts 'abc:h' opt; do
case "$opt" in
a)
echo "Processing option 'a'"
;;
b)
echo "Processing option 'b'"
;;
c)
arg="$OPTARG"
echo "Processing option 'c' with '${OPTARG}' argument"
;;
?|h)
echo "Usage: $(basename $0) [-a] [-b] [-c arg]"
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
Let’s execute the script and observe the output:
$ chmod +x parse-command-line-args.sh
$ ./parse-command-line-args.sh -a
Processing option 'a'
$ ./parse-command-line-args.sh -c test-value
Processing option 'c' with 'test-value' argument
$ ./parse-command-line-args.sh -ab
Processing option 'a'
Processing option 'b'
The getopts function reports an error in two scenarios —first, when an invalid option is used, and second, when an argument is not provided:
We can catch both of these error scenarios and show an appropriate error message to the user. Let’s modify the script to handle these scenarios:
#!/bin/bash
while getopts ':abc:h' opt; do
case "$opt" in
a)
echo "Processing option 'a'"
;;
b)
echo "Processing option 'b'"
;;
c)
arg="$OPTARG"
echo "Processing option 'c' with '${OPTARG}' argument"
;;
h)
echo "Usage: $(basename $0) [-a] [-b] [-c arg]"
exit 0
;;
:)
echo -e "option requires an argument.\nUsage: $(basename $0) [-a] [-b] [-c arg]"
exit 1
;;
?)
echo -e "Invalid command option.\nUsage: $(basename $0) [-a] [-b] [-c arg]"
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
Now, we’ll execute the script and check the error messages:
$ ./parse-command-line-args.sh -c
option requires an argument.
Usage: parse-command-line-args.sh [-a] [-b] [-c arg]
$ ./parse-command-line-args.sh -e
Invalid command option.
Usage: parse-command-line-args.sh [-a] [-b] [-c arg]
Note that we’ve updated optstring as well. Now it starts with the colon(:) character, which suppresses the default error message. Apart from this, we can use the OPTERR environment variable to alter error reporting behavior. The getopts function disables error reporting when the OPTERR variable is set to zero.
Sometimes, it’s convenient to use long command-line options to improve readability. However, getopts supports short command-line options only. We can use GNU’s getopt command to parse long command-line options:
#!/bin/bash
VALID_ARGS=$(getopt -o abg:d: --long alpha,beta,gamma:,delta: -- "$@")
if [[ $? -ne 0 ]]; then
exit 1;
fi
eval set -- "$VALID_ARGS"
while [ : ]; do
case "$1" in
-a | --alpha)
echo "Processing 'alpha' option"
shift
;;
-b | --beta)
echo "Processing 'beta' option"
shift
;;
-g | --gamma)
echo "Processing 'gamma' option. Input argument is '$2'"
shift 2
;;
-d | --delta)
echo "Processing 'delta' option. Input argument is '$2'"
shift 2
;;
--) shift;
break
;;
esac
done
In the above script:
Let’s execute the script and observe the output:
$ chmod +x parse-long-command-line-args.sh
$ ./parse-long-command-line-args.sh -a
Processing 'alpha' option
$ ./parse-long-command-line-args.sh --alpha
Processing 'alpha' option
$ ./parse-long-command-line-args.sh --delta test-value
Processing 'delta' option. Input argument is 'test-value'
$ ./parse-long-command-line-args.sh -g test-value
Processing 'gamma' option. Input argument is 'test-value'
In this tutorial, we discussed two methods we can use to parse Linux command-line arguments. First, we discussed bash‘s built-in function getopts. Then, we discussed GNU’s getopt command. We can use these examples in day-to-day life to make our shell scripts more robust.