在shell中用双引号字符串转义反引号

时间:2022-03-05 22:29:20

For the command: /usr/bin/sh -c "ls 1`" (a backquote after 1).

对于命令:/ usr / bin / sh -c“ls 1`”(1之后的反引号)。

How to make it run successfully? Adding a backslash before "`" does not work. ` is a special char as we know, and I tried surrounding it with single quote too (/usr/bin/sh -c "ls 1'`'"), but that doesn't work either.

如何让它成功运行?在“`”之前添加反斜杠不起作用。 `是我们所知道的一个特殊字符,我也尝试使用单引号将其包围(/ usr / bin / sh -c“ls 1'`'”),但这也不起作用。

The error always are:

错误始终是:

% /usr/bin/sh -c "ls 1\`"
Unmatched `

3 个解决方案

#1


35  

You need to escape the backtick, but also escape the backslash:

你需要逃避反引号,但也逃避反斜杠:

$ touch 1\`
$ /bin/sh -c "ls 1\\\`"
1`

The reason you have to escape it "twice" is because you're entering this command in an environment (such as a shell script) that interprets the double-quoted string once. It then gets interpreted again by the subshell.

您必须“两次”转义它的原因是因为您在一个解释双引号字符串的环境(例如shell脚本)中输入此命令。然后由子shell再次解释它。

You could also avoid the double-quotes, and thus avoid the first interpretation:

你也可以避免使用双引号,从而避免第一种解释:

$ /bin/sh -c 'ls 1\`'
1`

Another way is to store the filename in a variable, and use that value:

另一种方法是将文件名存储在变量中,并使用该值:

$ export F='1`'
$ printenv F
1`
$ /bin/sh -c 'ls $F'  # note that /bin/sh interprets $F, not my current shell
1`

And finally, what you tried will work on some shells (I'm using bash, as for the above examples), just apparently not with your shell:

最后,你尝试过的东西会在一些shell上运行(我正在使用bash,就像上面的例子一样),显然不是你的shell:

$ /bin/sh -c "ls 1'\`'"
1`
$ csh  # enter csh, the next line is executed in that environment
% /bin/sh -c "ls 1'\`'"
Unmatched `.

I strongly suggest you avoid such filenames in the first place.

我强烈建议你首先避免使用这些文件名。

#2


2  

Use single quotes instead:

改为使用单引号:

/usr/bin/sh -c 'ls 1\`'

#3


0  

 /usr/bin/sh -c "ls '1\`'"

#1


35  

You need to escape the backtick, but also escape the backslash:

你需要逃避反引号,但也逃避反斜杠:

$ touch 1\`
$ /bin/sh -c "ls 1\\\`"
1`

The reason you have to escape it "twice" is because you're entering this command in an environment (such as a shell script) that interprets the double-quoted string once. It then gets interpreted again by the subshell.

您必须“两次”转义它的原因是因为您在一个解释双引号字符串的环境(例如shell脚本)中输入此命令。然后由子shell再次解释它。

You could also avoid the double-quotes, and thus avoid the first interpretation:

你也可以避免使用双引号,从而避免第一种解释:

$ /bin/sh -c 'ls 1\`'
1`

Another way is to store the filename in a variable, and use that value:

另一种方法是将文件名存储在变量中,并使用该值:

$ export F='1`'
$ printenv F
1`
$ /bin/sh -c 'ls $F'  # note that /bin/sh interprets $F, not my current shell
1`

And finally, what you tried will work on some shells (I'm using bash, as for the above examples), just apparently not with your shell:

最后,你尝试过的东西会在一些shell上运行(我正在使用bash,就像上面的例子一样),显然不是你的shell:

$ /bin/sh -c "ls 1'\`'"
1`
$ csh  # enter csh, the next line is executed in that environment
% /bin/sh -c "ls 1'\`'"
Unmatched `.

I strongly suggest you avoid such filenames in the first place.

我强烈建议你首先避免使用这些文件名。

#2


2  

Use single quotes instead:

改为使用单引号:

/usr/bin/sh -c 'ls 1\`'

#3


0  

 /usr/bin/sh -c "ls '1\`'"