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

标签 bash command-line-arguments

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

例如,我的脚本名称是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/3190818/

相关文章:

java - 从命令行获取参数

c# - Windows 窗体应用程序命令行参数

c++ - "Conditional"解析命令行参数

linux - 多个 ssh 命令中的 cat 不起作用

linux - 终端中的脚本输出与 MOTD 输出不同

php - 如何修复 Laravel 中的 "php artisan migrate"错误

python - 模仿 AWS CLI/gcloud 命令行参数

BASH:百分比变化——如何计算?如何在没有 bc 的情况下获得绝对值?

bash - 使用 Shell 脚本在新文件中打印带有一些文本的日期

java - 更改程序以接受命令行参数