Pass command line parameters to a program inside the shell script
The usual way would be to save a copy of arg1 ("$1"
) and shift the parameters by one, so you can refer to the whole list as "$@"
:
#!/bin/sh
arg1="$1"
shift 1
/path/to/a/program "$@"
bash has some array support of course, but it is not needed for the question as posed.
If even arg1 is optional, you would check for it like this:
if [ $# != 0 ]
then
arg1="$1"
shift 1
fi
You can slice the positional parameters using parameter expansion. The syntax is:
${parameter:offset:length}
If length
is omitted it is taken as till the last value.
As you were to pass from second to last arguments, you need:
${@:2}
Example:
$ foo() { echo "${@:2}" ;}
$ foo bar spam egg
spam egg