(I know this is an old post, but none of the answers actually answered the question.)
Use the BASH_ARGV array. It contains the arguments passed to the invoking script in reverse order (i.e., it's a stack with the top at index 0). You may have to turn on extended debugging in the shebang (e.g., #!/bin/bash -O extdebug
) or with shopt
(e.g., shopt -s extdebug
), but it works for me in bash 4.2_p37 without it turned on.
From man bash
:
An array variable containing all of the parameters in the current bash execution call stack. The final parameter of the last subroutine call is at the top of the stack; the first parameter of the initial call is at the bottom. When a subroutine is executed, the parameters supplied are pushed onto BASH_ARGV. The shell sets BASH_ARGV only when in extended debugging mode….
Here's a function I use to print all arguments in order on a single line:
# Print the arguments of the calling script, in order.
function get_script_args
{
# Get the number of arguments passed to this script.
# (The BASH_ARGV array does not include $0.)
local n=${#BASH_ARGV[@]}
if (( $n > 0 ))
then
# Get the last index of the args in BASH_ARGV.
local n_index=$(( $n - 1 ))
# Loop through the indexes from largest to smallest.
for i in $(seq ${n_index} -1 0)
do
# Print a space if necessary.
if (( $i < $n_index ))
then
echo -n ' '
fi
# Print the actual argument.
echo -n "${BASH_ARGV[$i]}"
done
# Print a newline.
echo
fi
}