gpt4 book ai didi

bash - 在 Bash shell 脚本中传播所有参数

转载 作者:行者123 更新时间:2023-11-29 08:36:48 25 4
gpt4 key购买 nike

我正在编写一个调用另一个脚本的非常简单的脚本,我需要将参数从我当前的脚本传播到我正在执行的脚本。

例如,我的脚本名称是foo.sh 并调用bar.sh

foo.sh:

bar $1 $2 $3 $4

如何在不明确指定每个参数的情况下执行此操作?

最佳答案

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

观察:

$ cat no_quotes.sh
#!/bin/bash
echo_args.sh $@

$ cat quotes.sh
#!/bin/bash
echo_args.sh "$@"

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

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

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

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

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

关于bash - 在 Bash shell 脚本中传播所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4824590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com