问题描述
我遇到一个Bash脚本传递参数给命令的问题。
I'm encountering an issue passing an argument to a command in a Bash script.
poc.sh:
#!/bin/bash
ARGS='"hi there" test'
./swap ${ARGS}
调剂:
#!/bin/sh
echo "${2}" "${1}"
电流输出是:
there" "hi
仅仅改变poc.sh(我相信换做我想要的,正确),我怎么poc.sh通过您好,并作为测试两个参数,用你好有周围没有引号呢?
Changing only poc.sh (as I believe swap does what I want it to correctly), how do I get poc.sh to pass "hi there" and test as two arguments, with "hi there" having no quotes around it?
推荐答案
嵌入式报价不保护的空白;他们从字面上对待。在使用数组庆典
:
Embedded quotes do not protect whitespace; they are treated literally. Use an array in bash
:
args=( "hi there" test)
./swap "${args[@]}"
在POSIX shell中,您使用评估
卡住(这就是为什么大多数的炮弹支持数组)。
In POSIX shell, you are stuck using eval
(which is why most shells support arrays).
args='"hi there" test'
eval "./swap $args"
像往常一样,是的非常的一定要知道的$ args
的内容并了解使用之前生成的字符串将如何解析评估
。
As usual, be very sure you know the contents of $args
and understand how the resulting string will be parsed before using eval
.
这篇关于击:阅读报价/正确地从一个字符串参数逃脱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!