Skip to content

Instantly share code, notes, and snippets.

@aejh
Last active June 11, 2021 07:56
Show Gist options
  • Save aejh/9dfc6886ab55aca5308f63eae3fc5c15 to your computer and use it in GitHub Desktop.
Save aejh/9dfc6886ab55aca5308f63eae3fc5c15 to your computer and use it in GitHub Desktop.
Shell script argument handling, quick n dirty
# For reference:
# $# The number of arguments
# $0 The program name
# $@ All arguments
# $1 .. $N Argument 1 .. N
while [ $# -gt 0 ]; do
# $1 contains next argument, process $1 here
echo $1
shift # Pops $1 off the argument stack
done
# or using a case statement
while [ $# -gt 0 ]; do
case "$1" in
"-v") VERBOSE=true;;
# More here
*) echo "Unknown argument: $1"; exit 1;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment