Propagate all arguments in a bash shell script

2019-01-20 07:18发布

I am writing a very simple script that calls another script, and I need to propagate the parameters from my current script to the script I am executing.

For instance, my script name is foo.sh and calls bar.sh

foo.sh:

bar $1 $2 $3 $4

How can I do this without explicitly specifying each parameter?

8条回答
放荡不羁爱自由
2楼-- · 2019-01-20 07:51

Use "$@" (works for all POSIX compatibles).

[...] , bash features the "$@" variable, which expands to all command-line parameters separated by spaces.

From Bash by example.

查看更多
来,给爷笑一个
3楼-- · 2019-01-20 07:58

Works fine, except if you have spaces or escaped characters. I don't find the way to capture arguments in this case and send to a ssh inside of script.

This could be useful but is so ugly

_command_opts=$( echo "$@" | awk -F\- 'BEGIN { OFS=" -" } { for (i=2;i<=NF;i++) { gsub(/^[a-z] /,"&@",$i) ; gsub(/ $/,"",$i );gsub (/$/,"@",$i) }; print $0 }' | tr '@' \' )
查看更多
登录 后发表回答