将 bash 脚本参数传递给子进程不变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1695819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:22:55  来源:igfitidea点击:

Pass bash script parameters to sub-process unchanged

bashscriptingparameters

提问by EMP

I want to write a simple bash script that will act as a wrapper for an executable. How do I pass all the parameters that script receives to the executable? I tried

我想编写一个简单的 bash 脚本,作为可执行文件的包装器。如何将脚本接收到的所有参数传递给可执行文件?我试过

/the/exe $@

but this doesn't work with quoted parameters, eg.

但这不适用于带引号的参数,例如。

./myscript "one big parameter"

runs

/the/exe one big parameter

which is not the same thing.

这不是一回事。

回答by ddaa

When a shell script wraps around an executable, and if you do not want to do anything after the executable completes (that's a common case for wrapper scripts, in my experience), the correct way to call the executable is:

当 shell 脚本环绕可执行文件时,如果您不想在可执行文件完成后执行任何操作(根据我的经验,这是包装脚本的常见情况),调用可执行文件的正确方法是:

exec /the/exe "$@"

The execbuilt-in tells the shell to just give control to the executable without forking.

exec内置告诉shell只是把控制权交给可执行没有分叉。

Practically, that prevents a useless shell process from hanging around in the system until the wrapped process terminates.

实际上,这可以防止无用的 shell 进程在系统中徘徊,直到被包装的进程终止。

That also means that no command can be executed after the execcommand.

这也意味着在命令之后不能执行任何exec命令。

回答by sth

You have to put the $@in quotes:

你必须把$@引号:

/the/exe "$@"