Skip to content

Instantly share code, notes, and snippets.

@MartenH
Created April 2, 2020 07:12
Show Gist options
  • Save MartenH/0b5482db82a19bc795aa9873e1d7cfcf to your computer and use it in GitHub Desktop.
Save MartenH/0b5482db82a19bc795aa9873e1d7cfcf to your computer and use it in GitHub Desktop.
bash, arguments
#!/bin/bash
if [[ "$1" = "" ]]; then
echo "Usage:"
echo " -a foo|bar Select foo or bar"
echo " -v verbose (set -x)"
echo " --arg1 One arg"
echo ""
echo "Example:
echo " ./arguments.sh -a foo --arg1"
exit 0
fi
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-a|--alphabet)
export selected_a="$2"
shift # past argument
shift # past value
;;
--arg1
export hello=y
shift # past argument
;;
-v|--verbose)
export verbose=y
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment