Created
November 11, 2015 09:37
-
-
Save rciorba/514fd75f4a6471d44d71 to your computer and use it in GitHub Desktop.
bash argument parsing example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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".