【发布时间】:2016-09-20 08:20:28
【问题描述】:
假设./program是一个只打印出参数的程序;
$ ./program "Hello there"
Hello there
如何正确地从变量中传递带引号的参数?我正在尝试这样做;
$ args='"Hello there"'
$ echo ${args}
"Hello there"
$ ./program ${args}
Hello there # This is 1 argument
但是,当我遍历一个变量时,args 中的引号似乎被忽略了,所以我得到了;
$ args='"Hello there"'
$ echo ${args}
"Hello there"
$ ./program ${args}
"Hello there" # This is 2 arguments
是否可以让 bash 将引号视为我自己在第一个代码块中输入它们?
【问题讨论】:
-
./program "${args}" -
@redneb 试过了,它仍然将其视为 2 个参数(
"Hello和there") -
然后
program坏了。 -
你怎么知道(或者你为什么认为)
program将"$args"的扩展视为两个参数? -
@chepner 我的评论是错误的。它确实将其视为单个 arg,但由于某种原因,当您通过变量时,arg 为
"Hello there",而当您通过命令行时,arg 为Hello There。无法弄清楚如何绕过这些报价(请参阅下面的答案中的我的 cmets)