
There are various ways to pass data into a Bash shell script.
- Pass in arguments (also known as positional arguments)
- Pass in options (also known as key value pairs)
- Pass in flags
Pass in arguments
In this example, "foo" and "bar" are passed into the example.sh script.
example.sh foo bar
In the example.sh script, foo will be accessed using $1 and bar will be accessed using $2, like this.
#!/bin/bash
echo "\$0 = $0"
echo "\$1 = $1"
echo "\$2 = $2"
Invoking this script will return the following output.
$0 = example.sh
$1 = foo
$2 = bar
Likewise, the argument will also be stored in the $@ variable.
echo "$@"
And in the $* variable.
echo "$*"
Both $@ and $* should return the following.
foo bar
And here is a very simple case statement that will echo "got foo" or "got bar" if $1 is foo or bar, else "opps" will be returned.
case "$1" in
foo) echo "got foo";;
bar) echo "got bar";;
*) echo "opps";;
esac
Pass in options
A case statement can be used to create both command line options and flags. An option must be followed by a value. In this example, the --name option is followed by a value of Jeremy.
~]# example.sh --name Jeremy
A flag is not followed by a value on the command line. In this example, the --help flag is used. A flag will be associated with a value in the case statement.
~]# example.sh --help
One of the most practical uses of a case statement is to be able to create command line options. For example, lets say you have a bash script and you want options such as -n or --name and -o or --occupation.
example.sh -n Jeremy -o Engineer
First, you will create a variable that will contain the short and long verision of the command line options. In this example n:o: creates the -n and -o options, and name:,occupation: creates the --name and --occupation options.
Next you evaluate the $ARGS variable.
Then a while loop is used to associate each command line option with it's value. For example, if -n Jeremy or --name Jeremy is used on the command line, then a variable called name will contain a value of Jeremy.
You can then do something based on the value associated with the command line options.
#!/bin/bash
ARGS=$(getopt --options n:o: --long "name:,occupation:" -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
-n|--name)
name="$2"
shift 2;;
-o|--occupation)
occupation="$2"
shift 2;;
--)
break;;
*)
printf "Unknown option %s\n" "$1"
exit 1;;
esac
done
echo "Your name is $name and your occupation is $occupation"
Running this script should return something like this.
[john.doe@localhost ~]$ bash example.sh --name Jeremy --occupation "Computer Programmer"
Your name is Jeremy and your occupation is Computer Programmer
If you only want long options, set -o or --options to an empty string (--option '')
#!/bin/bash
ARGS=$(getopt --options '' --long "name:,occupation:" -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
-n|--name)
name="$2"
shift 2;;
-o|--occupation)
occupation="$2"
shift 2;;
--)
break;;
*)
printf "Unknown option %s\n" "$1"
exit 1;;
esac
done
echo "Your name is $name and your occupation is $occupation"
Pass in flags
You can also create flags. For example, let's say you want to have flags for -v or --verbose and -h or --help.
~]# example.sh -v -h
First, you will create a variable that will contain the short and long verision of the command line flags. In this example, vh creates the -v and -h options, and verbose,help creates the --verbose and --help options. The difference between an option and a flag is that an option has the : character, and a flag does not.
Next you evaluate the $ARGS variable.
You will also want to set default values for each flag. In this example, the default value is "false".
Then a while loop is used to update the verbose and help value to "true" if the flags are used.
Finally, tou can then use an if statement to do something with the verbose and help flags.
#!/bin/bash
ARGS=$(getopt --options vh --long "verbose,help" -- "$@")
eval set -- "$ARGS"
verbose="false"
help="false"
while true; do
case "$1" in
-v|--verbose)
verbose="true"
shift;;
-h|--help)
help="true"
shift;;
--)
break;;
*)
printf "Unknown option %s\n" "$1"
exit 1;;
esac
done
if [ $help == true ]; then
echo "Here is some helpful information"
fi
Arguments and Options
Let's say you want to include an argument (foo) followed by an option (--name john.doe).
example.sh foo --name john.doe
In this scenario, you may just need a simple if statement to get the job done.
if [ ! -z "$1" ]; then
argument="$1"
fi
if [ ! -z "$2" ]; then
name="$3"
fi
Single dash option
By default, the --long flag allows you to use "long" command line options such as --name instead of -n. The -a or --alternative flag allows a single dash long option, such as -name.
#!/bin/bash
ARGS=$(getopt --alternative --options n:o: --long "name:,occupation:" -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
-n|--name)
name="$2"
shift 2;;
-o|--occupation)
occupation="$2"
shift 2;;
--)
break;;
*)
printf "Unknown option %s\n" "$1"
exit 1;;
esac
done
echo "Your name is $name and your occupation is $occupation"
Did you find this article helpful?
If so, consider buying me a coffee over at