Skip to content

Instantly share code, notes, and snippets.

@everdark
Last active August 29, 2015 14:06
Show Gist options
  • Save everdark/59837ce595c70d5c6a21 to your computer and use it in GitHub Desktop.
Save everdark/59837ce595c70d5c6a21 to your computer and use it in GitHub Desktop.
shell scripting playground: command line arguments
#!/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