Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Parse command-line arguments in bash easily
# ./ --arg1 value1 --arg2 value2 --arg3 value3
# ./ -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:
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.