Command line option and flags using case statement in Linux Bash

Home > Search
  by

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

 


Command line options

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.

ARGS=$(getopt -a --options n:o: --long "name:,occupation:" -n "example" -- "$@")

 

Next you evaluate the $ARGS variable.

eval set -- "$ARGS"

 

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.

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

 

You can then do something based on the  value associated with the command line options.

if [ -z $name ]
then
  echo "Your name is $name"
fi

 


Command line 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.

ARGS=$(getopt -a --options vh --long "verbose,help" -n "example" -- "$@")

 

Next you evaluate the $ARGS variable.

eval set -- "$ARGS"

 

You will also want to set default values for each flag. In this example, the default value is "false".

verbose="false"
help="false"

 

Then a while loop is used to update the verbose and help value to "true" if the flags are used.

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

 

You can then use an if statement to do something with the verbose and help flags.

if [ $help == true ]
then
  echo "Here is some helpful information"
fi

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments