Skip to content

Instantly share code, notes, and snippets.

@rciorba
Created November 11, 2015 09:37
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 rciorba/514fd75f4a6471d44d71 to your computer and use it in GitHub Desktop.
Save rciorba/514fd75f4a6471d44d71 to your computer and use it in GitHub Desktop.
bash argument parsing example
#!/bin/bash
UNCONSUMED=()
while [[ $# > 0 ]]
do
key="$1"
case $key in
-l|--lib)
LIBPATH="$2"
shift # past argument
;;
*)
UNCONSUMED+=("$key")
;;
esac
shift # past argument or value
done
set -- ${UNCONSUMED[@]} # UNCONSUMED becomes our new $@
@lionello
Copy link

Doesn't appear to handle positional arguments with spaces. args.sh "a b" will end up having $1 set to "a" and $2 set to "b".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment