Skip to content

Instantly share code, notes, and snippets.

@virullius
Created October 28, 2014 15:54
Show Gist options
  • Save virullius/241b6937be5570c2b849 to your computer and use it in GitHub Desktop.
Save virullius/241b6937be5570c2b849 to your computer and use it in GitHub Desktop.
Shell argument examples (bash, sh, zsh)
echo "\$# is the number of arguments"
echo "$# arguments"
echo
echo "\$* is list of arguments joined into a string"
echo $*
echo
echo "\$@ is a sequence of strings, well suited to a for loop"
echo $@
echo
echo "typical for loop to iterate all arguments
(does not handle arguments with spaces)
----------
for ARG in \$@
do
echo \$ARG
done
----------"
for ARG in $@
do
echo $ARG
done
echo "----------"
echo
echo "another way to iterate arguments
(This one handles arguments with spaces properly)
----------
while [ \$# -gt 0 ]
do
echo \$1
shift
done"
echo "----------"
while [ $# -gt 0 ]
do
echo $1
shift
done
echo "----------"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment