假设./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视为引用,就好像我自己在第一个代码块中输入它们一样?
解决方法:
我不知道你从哪里获得了程序,但它看起来已经坏了.这是在bash中编写它的正确方法:
#!/bin/bash
for arg in "$@"; do
echo "$arg"
done
这将在单独的行中打印每个参数以使它们更容易区分(当然,包含换行符的参数会有问题,但我们不会传递这样的参数).
将上述内容保存为程序并为其授予执行权限后,请尝试以下操作:
$args='"Hello there"'
$./program "${args}"
"Hello there"
而
$args='"Hello there"'
$./program ${args}
"Hello
there"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。