简体   繁体   English

将命令参数中的单引号转至sh -c

[英]Escape single quote in command argument to sh -c

I know this question might sound familiar and may be similar to other questions like how to escape quotes in command argument to sh -c? 我知道这个问题听起来很耳熟,可能与其他问题类似,例如如何在sh -c的命令参数中转义引号? and many others. 还有很多其他 But none of the methods marked as answer to these question work for me. 但是,没有任何方法被标记为这些问题的答案对我有用。 So, please bear with me. 所以,请忍受我。

I am trying to send a command to another terminal. 我正在尝试向另一个终端发送命令。 I've learned that I need to use sh -c to send the entire command in once. 我了解到我需要使用sh -c一次发送整个命令。 The command itself is to compress a file using 7z. 该命令本身就是使用7z压缩文件。 Here is an example: 这是一个例子:

7z a Aa.zip Bb.txt

so the entire command would be 所以整个命令是

sh -c '7z a Aa.zip Bb.txt'

This works without any issue. 这没有任何问题。 The problem is when there is single quote ( ' ) in the filename to be compressed, eg B'b.txt . 问题是当文件名中有单引号( ' )要压缩时,例如B'b.txt So, the command becomes 因此,命令变为

sh -c '7z a Aa.zip B'b.txt'

which does not run in terminal. 不在终端上运行。

These are the commands that I tried with no luck: 这些是我没有运气尝试过的命令:

sh -c '7z a Aa.zip B'b.txt'
sh -c '7z a Aa.zip B\'b.txt'
sh -c '7z a Aa.zip B'"'"'b.txt'
sh -c '7z a Aa.zip "B'b.txt"'
sh -c '7z a Aa.zip \"B\'b.txt\"'
sh -c '7z a Aa.zip \"B'b.txt\"'
sh -c '7z a Aa.zip B'\''b.txt'

Running these commands result in either this error: 运行这些命令会导致以下错误:

Syntax error: Unterminated quoted string

or waiting for input 或等待输入

>

which I then cancel using Ctrl+c. 然后我使用Ctrl + c取消。

I also tried using a variable and then pass it to sh -c . 我也尝试使用变量,然后将其传递给sh -c Again with no luck: 再次没有运气:

cmd="'7z a Aa.zip B'b.txt'"
echo $cmd
'7z a Aa.zip B'b.txt'
sh -c $cmd
a: 1: a: Syntax error: Unterminated quoted string

Please let me know what I am doing wrong? 请让我知道我在做什么错?

You're looking for: 您正在寻找:

sh -c '7z a Aa.zip "B'\''b.txt"'

This: '\\'' is an escaped ' as a part of the string. 这: '\\''是转义的'作为字符串的一部分。 You need that for the sh command itself. sh命令本身需要它。 Once you've started running the command, leaving the ' unmatched causes a problem, so you need to put it inside of a string. 一旦你开始运行的命令,离开'无与伦比导致出现问题,所以你需要把它放到字符串里。

I'll suggest reading through this shell command language article. 我建议您通读这篇shell命令语言文章。 For your case, specifically 对于您的情况,特别是

"A backslash cannot be used to escape a single-quote in a single-quoted string. An embedded quote can be created by writing, for example: "'a'\\''b'", which yields "a'b". A single token can be made up of concatenated partial strings containing all three kinds of quoting or escaping, thus permitting any combination of characters." “反斜杠不能用于对单引号引起来的单引号进行转义。嵌入的引号可以通过编写来创建,例如:“'a'\\” b'“,这会产生” a'b“。一个令牌可以由包含所有三种引号或转义的连接的部分字符串组成,从而允许字符的任何组合。”

Hope it'll work for you. 希望对您有用。

sh -c "7z a Aa.zip B'b.txt"

or 要么

sh -c '7z a Aa.zip B'"'"'b.txt'

Or just, you know, use a (bash or zsh) array. 或者,您知道,使用(bash或zsh)数组。

cmd=(7z a Aa.zip B'b.txt)
sh -c "${cmd[@]}"

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

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