可变内行情将被忽略 [英] Quotes inside a variable are ignored
问题描述
我想从这样的变量传递一个参数的rsync
:
I want to pass an argument to rsync
from a variable like this:
myopts='-e "ssh -p 1234" -a'
rsync $myopts 192.168.0.1:/a /a
有关某种原因,这是行不通的。我甚至发现此网页,它说,它不会工作:
For some reason this does not work. I even found this webpage where it says that it won't work:
### NO NO NO: this passes three strings:
### (1) "my
### (2) multiword
### (3) argument"
MYARG="\"my multiword argument\""
somecommand $MYARG
### THIS IS NOT (!!!!) THE SAME AS ###
command "my multiword argument"
### YOU NEED ###
MYARG="my multiword argument"
command "$MYARG"
遗憾的是它并没有说的为什么的它不会工作。
包含的rsync
调用的脚本是由很多其他脚本的使用,所以我只能改变它在兼容的方式。不工作的一个解决方案是使用数组:
The script containing the rsync
call is used by a lot of other scripts, so I can only change it in a compatible way. A solutions that doesn't work is using an array:
myopts=('-a' '-v' '-z') # new way, would work
myopts='-a -v -z' # old way, breaks
rsync "${myopts[@]}" 192.168.0.1:/a /a
这是可变的选项被完全忽略。
The options from the variable are completely ignored.
推荐答案
变扩建后,这样做了唯一的处理是分词和通配符扩展功能(这些都不是,如果变量是内双引号完成),而不是报价处理。您需要使用评估
如果你想在命令行解析的所有步骤重做:
After variable expansion, the only processing that's done is word splitting and wildcard expansion (and neither of these is done if the variable is inside doublequotes), not quote processing. You need to use eval
if you want all steps of command line parsing to be redone:
eval "rsync $MYOPTS 192.168.0.1:/a /a"
这篇关于可变内行情将被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!