简体   繁体   English

linux命令行中双引号字符串内的单引号

[英]Single quote inside of double quoted string on linux command line

I have the following command 我有以下命令

cmd '"asdf" "a'sdf"'

I need to surround the arguments with single quotes only. 我需要用单引号括起参数。 cmd doesnt work with double quotes for some reason I dont know. 由于某些原因我不知道cmd不能使用双引号。 The above command doesnt work because the single in the middle terminates the first single quote. 上面的命令不起作用,因为中间的单个句点终止了第一个单引号。 If I escape, to the following 如果我逃脱,到下面

cmd '"asdf" "a\'sdf"'

It still doesnt work. 它仍然无法正常工作。 How do I get this working? 我该如何工作?

According to the bash man page: 根据bash手册页:

   Enclosing  characters in single quotes preserves the literal value
   of each character within the quotes.  A single quote may not occur
   between single quotes, even when preceded by a backslash.

So the answer is, you can't include a single quote within a single-quoted string no matter how you try. 所以答案是,无论您如何尝试,都不能在单引号字符串中包含单引号。 But depending on how your script is set up you may be able to use '\\'' , which will end the first single quote, escape the second, and start another single-quoted string with the third. 但是根据你的脚本设置方式,你可以使用'\\'' ,它将结束第一个单引号,转义第二个,并用第三个引用另一个单引号字符串。

A long long time ago, a mentor suggested I use constructs like '"asdf" "a'"'"'sdf"' . 很久很久以前,一位导师建议我使用像'"asdf" "a'"'"'sdf"' It works, but it's bizarre to look at. 它有效,但看起来很奇怪。

Since you can't put single quotes inside single quotes, escaping them like '"asdf" "a'\\''sdf' may be the way to go. 因为你不能在单引号中放入单引号,所以像'"asdf" "a'\\''sdf'那样转义它们可能是要走的路。

Note that you can also use printf and variables interactively or within a shell script. 请注意,您还可以交互方式或在shell脚本中使用printf和变量。 With most shells (you haven't specified what you're using), you should get the similar results to this: 对于大多数shell(你没有指定你正在使用的),你应该得到类似的结果:

$ fmt='"asdf" "a%ssdf"\n'
$ printf "$fmt" "'"
"asdf" "a'sdf"
$ 

or you could even include the single quote using its ASCII value: 或者您甚至可以使用其ASCII值包含单引号:

$ fmt='"asdf" "a\047sdf"\n'
$ printf "$fmt"
"asdf" "a'sdf"
$ 

or in csh: 或者在csh中:

% set fmt='"asdf" "a\047sdf"\n'
% printf "$fmt"
"asdf" "a'sdf"
%

This is shell-independent because if your shell doesn't have a printf command built in (as Bash has), then the command will most likely exist as a separate binary in /bin or /usr/bin. 这与shell无关,因为如果你的shell没有内置的printf命令(如Bash所有),那么该命令很可能作为/ bin或/ usr / bin中的单独二进制文件存在。

I don't know your use case, so it's difficult to come up with a solution that I know will be applicable. 我不知道你的用例,所以我很难想出一个我知道适用的解决方案。

Well, you can always use autocomplete to help you find the answer. 好吧,您可以随时使用自动填充功能来帮助您找到答案。

For instance, if I have a file with an apostrophe in it, but I want to surround the path with single quotes, I can do this (using cat as an example command where the file is named sam's file.txt ): 例如,如果我有一个包含撇号的文件,但我想用单引号括起路径,我可以这样做(使用cat作为示例命令,其中文件名为sam's file.txt ):

cat 'sam[press tab here]

and it autocompletes to this, for me: 对我来说,它自动完成了这个:

cat 'sam'\''s\ file.txt

So, apparently it is possible. 所以,显然这是可能的。 Another answerer mentioned the '\\'' thing first, but I figured I'd tell you one way to try to figure it out if that's not working for you (and I thought I'd be another witness to tell you the other answer seems to work). 另一位回答者首先提到了'\\''事情,但我想我会告诉你一种方法,如果那对你不起作用就试着弄明白(而且我认为我会成为另一个证人,告诉你另一个答案似乎上班)。

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

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