Last active
August 29, 2015 14:04
-
-
Save zipizap/a5f4bea98b278519b02e to your computer and use it in GitHub Desktop.
Example of how to parse mutliple arguments in bash
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 | |
[[ $# -gt 1 ]] || { echo "Usage: $0 <arg1> <arg2> ... <argN>"; exit 255; } | |
echo 'Dont forget to surround the "$@" with the double-quotes '' so that it does not divide arguments that contain spaces.' | |
echo 'Samewise, you also need to surround the "$ARG_I" var' | |
for ARG_I in "$@"; do | |
echo '$@ = '"$@" | |
echo '$ARG_I = '"$ARG_I" | |
done | |
# paulo@lnxp:/tmp$ bash a.sh "Arg1" "I'm argument 2 with spaces" "And Im arg3" | |
# $@ = Arg1 I'm argument 2 with spaces And Im arg3 | |
# $ARG_I = Arg1 | |
# $@ = Arg1 I'm argument 2 with spaces And Im arg3 | |
# $ARG_I = I'm argument 2 with spaces | |
# $@ = Arg1 I'm argument 2 with spaces And Im arg3 | |
# $ARG_I = And Im arg3 | |
# | |
# NOTE: | |
# Dont forget to surround the "$@" with the double-quotes so that it does not divide arguments that contain spaces. | |
# Samewise, you also need to surround the "$ARG_I" var |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment