bash 如何在双引号内转义双引号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3834839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:42:53  来源:igfitidea点击:

How to escape a double quote inside double quotes?

bashquotes

提问by Sean Nguyen

Can anybody show me how to escape double quote inside a double string in bash?

谁能告诉我如何在 bash 中的双字符串中转义双引号?

For example in my shell script

例如在我的 shell 脚本中

#!/bin/bash

dbload="load data local infile \"'gfpoint.csv'\" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY \"'\n'\" IGNORE 1 LINES"

I can't get the ENCLOSED BY '\"'with double quote escape correctly. I can't use single quote for my variable because I want to use variable $dbtable.

我无法ENCLOSED BY '\"'正确获得带双引号的转义。我不能对我的变量使用单引号,因为我想使用 variable $dbtable

回答by Peter

Use a backslash:

使用反斜杠:

echo "\""     # Prints one " character.

回答by kenorb

Simple example of escaping quotes in shell:

在 shell 中转义引号的简单示例:

$ echo 'abc'\''abc'
abc'abc
$ echo "abc"\""abc"
abc"abc

It's done by finishing already opened one ('), placing escaped one (\'), then opening another one (').

它是通过完成已经打开的一个('),放置逃脱的一个(\'),然后打开另一个(')来完成的。

Alternatively:

或者:

$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc

It's done by finishing already opened one ('), placing quote in another quote ("'"), then opening another one (').

它是通过完成已经打开的一个 ( '),将引号放在另一个引号 ( "'") 中,然后打开另一个 ( ') 来完成的。

More examples: Escaping single-quotes within single-quoted strings

更多示例:在单引号字符串中转义单引号

回答by George Vasiliou

I don't know why this old issue popped up today in the bash tagged listings, but just in case for future researchers, keep in mind that you can avoid escaping by using ascii codes of the chars you need to echo. Example:

我不知道为什么今天在 bash 标记的列表中会出现这个旧问题,但为了将来的研究人员,请记住,您可以通过使用需要回显的字符的 ascii 代码来避免转义。例子:

echo -e "this is \x22\x27\x22\x27\x22text\x22\x27\x22\x27\x22"                                                                                                          
this is "'"'"text"'"'" 

\x22is the ascii code (in hex) for double quotes and \x27for single quotes. Similarly you can echo any char.

\x22是双引号和\x27单引号的 ascii 代码(十六进制)。同样,您可以回显任何字符。

I suppose if we try to echo the above string with backslashes, we will need a messy two rows backslashed echo... :)

我想如果我们尝试用反斜杠回显上述字符串,我们将需要一个凌乱的两行反斜杠回显... :)

For variable assignment this is the equivalent :

对于变量赋值,这是等效的:

$ a=$'this is \x22text\x22'                                                                                                                                               
$ echo "$a"                                                                                                                                                               
this is "text"

If the variable is already set by another program , you can still apply double/single quotes with sed or similar tools. Example:

如果变量已经由另一个程序设置,您仍然可以使用 sed 或类似工具应用双引号/单引号。例子:

$ b="just another text here"
$ echo "$b"
just another text here
$ sed 's/text/"'
$ echo "Hello"', world!'
'"/' <<<"$b" #
Hello, world!
is a special sed operator just another "0" here #this is not what i wanted to be $ sed 's/text/\x22\x27
$ echo "I like to use" '"double quotes"' "sometimes"
\x27\x22/' <<<"$b" just another "'text'" here #now we are talking. You would normally need a dozen of backslashes to achieve the same result in the normal way.

回答by Beetle

Bash allows you to place strings adjacently, and they'll just end up being glued together.

Bash 允许您将字符串相邻放置,它们最终会粘在一起。

So this:

所以这:

I like to use "double quotes" sometimes

produces

产生

$ dbtable=example
$ dbload='load data local infile "'"'gfpoint.csv'"'" into '"table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '"'"'"' LINES "'TERMINATED BY "'"'\n'"'" IGNORE 1 LINES'
$ echo $dbload

The trick is to alternate between single and double-quoted strings as required. Unfortunately, it quickly gets very messy. For example:

诀窍是根据需要在单引号和双引号字符串之间交替。不幸的是,它很快变得非常混乱。例如:

load data local infile "'gfpoint.csv'" into table example FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "'\n'" IGNORE 1 LINES

produces

产生

#!/bin/bash
mystr="say \"hi\""


In your example, I would do it something like this:

在你的例子中,我会这样做:

echo -e $mystr

which produces the following output:

产生以下输出:

echo -e $(printf '%q' $mystr)


It's difficult to see what's going on here, but I can annotate it using Unicode quotes. The following won't work in bash – it's just for illustration:

很难看出这里发生了什么,但我可以使用 Unicode 引号对其进行注释。以下在 bash 中不起作用 - 仅用于说明:

dbload=load data local infile "'“'gfpoint.csv'”‘" into'“table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '”‘"'“' LINES”‘TERMINATED BY "'“'\n'”‘" IGNORE 1 LINES'

dbload=' load data local infile "'“ 'gfpoint.csv'”' " into'“ table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '”' "'“ ' LINES”' TERMINATED BY "'“ '\n'”' " IGNORE 1 LINES'

The quotes like “ ‘ ' ” in the above will be interpreted by bash. The quotes like " 'will end up in the resulting variable.

上面像“''”这样的引号将被bash解释。引号 like" '将在结果变量中结束。

If I give the same treatment to the earlier example, it looks like this:

如果我对前面的示例进行相同的处理,它看起来像这样:

$ echoI like to use"double quotes"'sometimes

$ echoI like to use' "double quotes"' sometimes

回答by Danny Hong

check out printf...

看看printf...

dqt='"'
echo "Double quotes ${dqt}X${dqt} inside a double quoted string"

Without using printf

不使用 printf

Double quotes "X" inside a double quoted string

output: say "hi"

输出:说“嗨”

Using printf

使用printf

#! /bin/csh -f

set dbtable = balabala

set dbload = "load data local infile "\""'gfpoint.csv'"\"" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '"\""' LINES TERMINATED BY "\""'\n'"\"" IGNORE 1 LINES"

echo $dbload
# load data local infile "'gfpoint.csv'" into table balabala FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "''" IGNORE 1 LINES

output: say \"hi\"

输出:说“嗨”

回答by 12oclocker

Store the double quote character as variable:

将双引号字符存储为变量:

##代码##

Output:

输出:

##代码##

回答by Trisha Chatterjee

Make use of $"string".

使用 $"string"。

In this example, it would be,

在这个例子中,它将是,

dbload=$"load data local infile \"'gfpoint.csv'\" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY \"'\n'\" IGNORE 1 LINES"

dbload=$"load data local infile \"'gfpoint.csv'\" into table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY \"'\n'\" IGNORE 1 LINES"

Note(from the man page):

注意(来自手册页):

A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.

以美元符号 ($"string") 开头的双引号字符串将使字符串根据当前语言环境进行翻译。如果当前语言环境是 C 或 POSIX,则忽略美元符号。如果字符串被翻译和替换,则替换是双引号。

回答by Shilv

add "\"before double quote to escape it, instead of \

"\"在双引号前添加以将其转义,而不是\

##代码##