Last active
August 29, 2015 14:06
-
-
Save everdark/59837ce595c70d5c6a21 to your computer and use it in GitHub Desktop.
shell scripting playground: command line arguments
This file contains hidden or 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 | |
[ $# -eq 0 ] && { | |
echo give me some arguments! | |
exit 0 | |
} | |
echo $0 $1 ${2:-default_value} ${3:+and a third arg exists!} | |
echo total number of arguments passed: $# | |
echo all arguments: $* | |
printf "all arguments as a single string: %s\n" "$*" | |
printf "all arguments as a list of seperate strings: %s\n" "$@" | |
echo loop for each argument and print and pop: | |
while [ $# -ne 0 ] | |
do | |
printf "\t%s\n" $1 | |
shift # pop out the first argument, so that $# becomes one less | |
done | |
echo now we have no argument left at all! $* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment