linux - bash 及其引用规则的问题

标签 linux bash shell quotes

我遇到了 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/

相关文章:

linux - 如何在 Linux 终端中创建电子邮件帐户/地址?

linux - 在 Linux 终端中, "grep -q '/dev/sda 1'/proc/mounts || ..."会做什么?

regex - 在 grep 中转义斜杠 "\"

linux - 将 base64 字符串解码为文件中的十六进制字符串并覆盖

linux - bash 获取硬件脚本输出到 .csv 文件

c - IPC套接字错误

linux - 如何从 JSON 输出文件中删除这些特殊字符

linux - Dumpbin.exe for Linux 查看导入

string - 在bash中提取两个字符之间的字符串

linux - Bash 时差计算