Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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:

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...