How do I forward parameters to other command in bash script?


Question

Inside my bash script, I would like to parse zero, one or two parameters (the script can recognize them), then forward the remaining parameters to a command invoked in the script. How can I do that?

1
131
3/15/2017 2:41:33 PM

Accepted Answer

Use the shift built-in command to "eat" the arguments. Then call the child process and pass it the "$@" argument to include all remaining arguments. Notice the quotes, they should be kept, since they cause the expansion of the argument list to be properly quoted.

204
4/29/2017 2:45:37 PM

bash uses the shift command:

e.g. shifttest.sh:

#!/bin/bash
echo $1
shift
echo $1 $2

shifttest.sh 1 2 3 produces

1
2 3

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon