This is an old revision of the document!


Print argument list for testing

snipplet:
debug, arguments
LastUpdate:
2013-03-23
Contributor:
Snappy (IRC), Dan Douglas
type:
snipplet

Sometimes you might find it useful to see how arguments passed to a program arrive there.

Check this script (save it as script file or make a function):

printf '"%b"\n' "$0" "$@" | nl -v0 -s": "

It uses the printf command to generate a list of arguments, even with escape sequences interpreted. This list is shown formatted by the nl(1) utility.

Another alternative with colorized output.

# Debugging function for colored display of argv.
# Optionally set OFD to the desired output file descriptor.
function args {
	if [[ ${BASH_VERSION+$-} == *x* ]]; then
		{ BASH_XTRACEFD=3 command set +x; } 3>/dev/null
		trap 'set -x' RETURN
	fi
 
	[[ $OFD == +([0-9]) ]] || return
 
	if [[ -t ${OFD:-2} ]]; then
		typeset -A 'clr=( [green]="$(tput setaf 2)" [sgr0]="$(tput sgr0)" )'
	else
		typeset clr
	fi
 
	if ! ${1+false}; then
		printf -- "${clr[green]}<${clr[sgr0]}%s${clr[green]}>${clr[sgr0]} " "$@"
		echo
	else
		echo 'no args.'
	fi >&"${OFD:-2}"
}

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
You could leave a comment if you were logged in.
  • snipplets/prargs.1364052488.txt
  • Last modified: 2013/03/23 15:28
  • by ormaaj