如何在Bash脚本中使用空格传递命令行参数 [英] How to pass command line argument with space in Bash scripts
本文介绍了如何在Bash脚本中使用空格传递命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设有一个脚本 s1
,我需要传递参数 $ 1
并赋值 foo bar
,其中带有空格。可以做到
Let's say there is one script s1
, and I need to pass argument $1
with value foo bar
, with space in it. This can be done
./ s1 foo bar
./s1 "foo bar"
但是,当我想在另一个脚本中运行上面的命令(例如 s2
)时,应该怎么放置呢?如果我按照上面的说法, foo bar
将被解释为两个参数(到s1)而不是一个。
however, when I want to run the above command in another script (say s2
), how should I put it? If I put it as above, foo bar
will be interpreted as two arguments (to s1) rather than one.
推荐答案
您可以尝试引用 $ 1
:
./s2 "$1"
这篇关于如何在Bash脚本中使用空格传递命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文