Simple Command-Line Argument Parser for Bash

Cpak
Mar 11, 2021

I had a need to pass command line arguments to a bash script. Here’s an easy solution that I came across that can interpret both short- and long- command-line arguments.

#!/bin/bashwhile [[ "$#" -gt 0 ]]; do
case $1 in
-p|--profile) PROFILE="$2"; shift;;
-r|--region) REGION="$2"; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
ecas
shift
done
echo "PROFILE IS ${PROFILE}"
echo "REGION IS ${REGION}"

Let’s test it

bash test.sh -p user -r US

--

--