简体   繁体   English

Bash Script单引号内的双引号

[英]Bash Script single quotes inside of double quotes inside of single quotes

Yeah, this is kind of messed up but I have to do something like the following (this is just a simplified example): 是的,这有点混乱,但是我必须执行以下操作(这只是一个简化的示例):

sudo -H -u user1 bash -c 'ssh -I /home/user1/.ssh/id_rsa -f user2@machine1.domain1.com “find ./ -name '*.txt'”'

I have a need to have single quotes which are inside of double quotes which are inside of single quotes. 我需要在单引号内的双引号内包含单引号。 Doesn't seem the standard '"'"' would work since the doubles would close the doubles rather than quoting a single. 似乎标准的'"'"'不起作用,因为双打会关闭双打而不是引用单引号。 Would it maybe be: "'"'""' for each single I need inside there? 可能是:我在那里需要的每个单身都用"'"'""'

I've tried just skipping the ' around the ssh command but of course that failed. 我试过只是在ssh命令周围跳过' ,但是当然失败了。

You don't need the bash -c layer; 您不需要bash -c层; just run ssh itself directly from sudo . 只是直接从sudo运行ssh本身。

sudo -H -u user1 ssh -I /home/user1/.ssh/id_rsa -f user2@machine1.domain1.com "find ./ -name \"*.txt\""

(The -I option should be unnecessary; if ssh is run as user1 , then the default key is ~user1/.ssh/id_rsa . You may have meant -i anyway; -I specifies a PKCS#11 library to use, not a private key.) -I选项应该是不必要的;如果sshuser1身份运行,则默认密钥为~user1/.ssh/id_rsa 。无论如何,您可能已经说过-i-I指定要使用的PKCS#11库,而不是私钥。)

That said, you cannot include single quotes inside a single-quoted string. 就是说,您不能在单引号中包含单引号。 When in doubt, use double quotes, and properly escape any nested double quotes. 如有疑问,请使用双引号,并正确转义任何嵌套的双引号。

chepner's answer is sufficient. chepner的答案就足够了。 If bash -c required, this solution which looks messy can be used: 如果需要bash -c ,则可以使用看起来混乱的此解决方案:

sudo -H -u user1 bash -c 'ssh -I /home/user1/.ssh/id_rsa -f user2@machine1.domain1.com "find ./ -name '"'"'*.txt'"'"'"'

explanation 说明

'... "find ./ -name '[first ' ended here]  "'" [second ' quote start]  '*.txt'[pattern]  "'"[second ' end] '"'[ " ended here]

I replaced ' with ' . 我换成''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM