Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

bash - 在bash shell脚本中传播所有参数(Propagate all arguments in a bash shell script)

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.sh)

foo.sh:

(foo.sh:)

bar $1 $2 $3 $4

How can I do this without explicitly specifying each parameter?

(如何在不显式指定每个参数的情况下执行此操作?)

  ask by Fragsworth translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use "$@" instead of plain $@ if you actually wish your parameters to be passed the same.

(如果您确实希望传递相同的参数,请使用"$@"而不是普通的$@ 。)

Observe:

(观察:)

$ cat foo.sh
#!/bin/bash
baz.sh $@

$ cat bar.sh
#!/bin/bash
baz.sh "$@"

$ cat baz.sh
#!/bin/bash
echo Received: $1
echo Received: $2
echo Received: $3
echo Received: $4

$ ./foo.sh first second
Received: first
Received: second
Received:
Received:

$ ./foo.sh "one quoted arg"
Received: one
Received: quoted
Received: arg
Received:

$ ./bar.sh first second
Received: first
Received: second
Received:
Received:

$ ./bar.sh "one quoted arg"
Received: one quoted arg
Received:
Received:
Received:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...