Skip to content

Instantly share code, notes, and snippets.

@awwsmm
Last active June 23, 2019 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save awwsmm/27d68da263f4d3ab5ecb8e37ebeb4c7f to your computer and use it in GitHub Desktop.
Save awwsmm/27d68da263f4d3ab5ecb8e37ebeb4c7f to your computer and use it in GitHub Desktop.
Easily print arguments passed to a `bash` script
#!/usr/bin/env bash
#-------------------------------------------------------------------------------
#
# print_args - easily inspect arguments passed to a bash script
#
# sources:
# https://unix.stackexchange.com/a/332126/183920
#
#-------------------------------------------------------------------------------
function print_args {
echo "arguments:"
local ii=1
for arg; do
printf " \$%u: '%s'\n" "$ii" "$arg"
((ii++))
done
}
# usage:
#
# $ print_args 3 "3.a" "hello world"
# arguments:
# $1: '3'
# $2: '3.a'
# $3: 'hello world'
function example {
echo "printing arguments passed to 'example'..."
printargs "$@"
echo "...done."
printf "\$2 = %s\n" "$2"
}
# example:
#
# $ example "a b" 5.5 ho-ho
# printing arguments passed to 'example'...
# arguments:
# $1: 'a b'
# $2: '5.5'
# $3: 'ho-ho'
# ...done.
# $2 = 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment