Skip to content

Instantly share code, notes, and snippets.

@zipizap
Last active August 29, 2015 14:04
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 zipizap/a5f4bea98b278519b02e to your computer and use it in GitHub Desktop.
Save zipizap/a5f4bea98b278519b02e to your computer and use it in GitHub Desktop.
Example of how to parse mutliple arguments in bash
#!/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