【发布时间】:2013-10-18 15:28:20
【问题描述】:
我对 bash 的引用规则有疑问,我不知道如何解决。 我想在某种配置文件中有一个变量,可以在其中指定命令行选项列表。就我而言,它用于 rsync: rsync 具有命令行选项“--rsh”,它允许您指定它在另一端使用的远程shell。例如:
rsync -v --rsh="ssh -p4711 -lhttp" /tmp/testfile remoteMachine.x:/tmp/testfile
这很完美!但是,如果您尝试在脚本中执行此操作,您希望允许在变量中指定所有选项,如下所示:
#!/bin/bash
OPTS="-v --rsh=\"ssh -p4711 -lhttp\""
rsync $OPTS testfile remoteMachine.x:/tmp/testfile
执行此脚本时,它将失败并出现以下错误:
rsync: -p4711: unknown option
这是因为 bash 做了一些讨厌的引用/转义,我不知道为什么(你可以看到 bash 用 'bash -x' 做了什么):
+ OPTS='-v --rsh="ssh -p4711 -lhttp"'
+ rsync -v '--rsh="ssh' -p4711 '-lhttp"' testfile remoteMachine.x:/tmp/testfile
我尝试了弱引号和强引号 (", ') 的多种组合,但没有任何效果...您能帮帮我吗,为什么 bash 会这样做以及如何解决这种奇怪的行为?
来自科隆的感谢和问候!
【问题讨论】:
-
it's quoting rules-- 引用规则的另一个问题:P -
Nirk,“它的”是正确的。
-
一些简单的阅读来支持@user000001的回答:BashFAQ/050 - I'm trying to put a command in a variable, but the complex cases always fail!
-
谢谢@glennjackman,读得很好:)