-
-
Save sethbergman/1fb0e343dbc97c2213fdac6da00249c6 to your computer and use it in GitHub Desktop.
Easily print arguments passed to a `bash` script
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
#!/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