我遇到了 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 这样做以及我如何解决这种奇怪的行为?
来自科隆的感谢和问候!
最佳答案
使用数组:
OPTS=(-v --rsh="ssh -p4711 -lhttp")
rsync "${OPTS[@]}" testfile remoteMachine.x:/tmp/testfile
关于linux - bash 及其引用规则的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453356/