Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.