Created
October 28, 2014 15:54
-
-
Save virullius/241b6937be5570c2b849 to your computer and use it in GitHub Desktop.
Shell argument examples (bash, sh, zsh)
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
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