如何通过命令行参数与Bash脚本空间(How to pass command line argume

2019-07-30 21:01发布

比方说,有一个脚本s1 ,和我需要传递参数$1值为foo bar ,里面有空间。 这是可以做到

./s1“富巴”

然而,当我想以另一个脚本运行上面的命令(比如s2 ),我应该怎么说呢? 如果我把它上面的, foo bar将被解释为两个参数(以S1),而不是一个。

Answer 1:

您可以尝试引用$1

./s2 "$1"


Answer 2:

使用单引号。

./script 'this is a line'

要考虑的变量替换使用双引号

./script "this is a line"



Answer 3:

怎么样:

./s1 foo\ bar

将这项工作?



文章来源: How to pass command line argument with space in Bash scripts