Skip to content

Instantly share code, notes, and snippets.

@emmanuelnk
Created November 21, 2019 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmanuelnk/5c29f1f118c0081b6d4c35406ac78c68 to your computer and use it in GitHub Desktop.
Save emmanuelnk/5c29f1f118c0081b6d4c35406ac78c68 to your computer and use it in GitHub Desktop.
Parse command-line arguments in bash easily
# ./my_script.sh --arg1 value1 --arg2 value2 --arg3 value3
# ./my_script.sh -a value1 -b value2 -c value3
#!/usr/bin/env bash
while [[ "$#" -gt 0 ]]; do case $1 in
-a|--arg1) var1="$2"; shift;;
-b|--arg2) var2="$2"; shift;;
-c|--arg3) var3="$2"; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done
echo "this is value1: $var1"
echo "this is value1: $var2"
echo "this is value1: $var3"
# references:
# https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/14203146
# https://gist.github.com/hfossli/4368aa5a577742c3c9f9266ed214aa58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment