为什么当我运行 sh -c [script] 时,它不接受任何位置参数?



我很困惑;我运行一个脚本,使用 sh -c ,但如果我想传递一个参数,它将被忽略。

例:

# script.sh
param=$1
echo "parameter is: " $param

如果我运行它

sh -c ./script.sh 你好

我在输出中什么也没得到

为什么会这样?我怎样才能避免这种情况?

这将适用于您:

sh -c "./script.sh hello"

如果以这种方式运行它:

sh -c ./script.sh hello

hello成为sh的第二个参数,./script.sh运行时没有参数。

-c开关接受单个参数。 外壳将自行执行解析。 例如

sh -c './script.sh hello'

最新更新