问题
Can anybody show me how to escape double quote inside a double string in bash?
For example in my shell script
#!/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.
回答1:
Use a backslash:
echo "\"" # Prints one " character.
回答2:
Simple example of escaping quotes in 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
回答3:
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:
echo -e "this is \x22\x27\x22\x27\x22text\x22\x27\x22\x27\x22"
this is "'"'"text"'"'"
\x22 is the ascii code (in hex) for double quotes and \x27 for single quotes. Similarly you can echo any char.
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:
$ b="just another text here"
$ echo "$b"
just another text here
$ sed 's/text/"'\0'"/' <<<"$b" #\0 is a special sed operator
just another "0" here #this is not what i wanted to be
$ sed 's/text/\x22\x27\0\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.
回答4:
Bash allows you to place strings adjacently, and they'll just end up being glued together.
So this:
$ echo "Hello"', world!'
produces
Hello, world!
The trick is to alternate between single and double-quoted strings as required. Unfortunately, it quickly gets very messy. For example:
$ echo "I like to use" '"double quotes"' "sometimes"
produces
I like to use "double quotes" sometimes
In your example, I would do it something like this:
$ 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
which produces the following output:
load data local infile "'gfpoint.csv'" into table example FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "'\n'" IGNORE 1 LINES
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:
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.
If I give the same treatment to the earlier example, it looks like this:
$ echo“I like to use”‘"double quotes"’“sometimes”
回答5:
check out printf...
#!/bin/bash
mystr="say \"hi\""
Without using printf
echo -e $mystr
output: say "hi"
Using printf
echo -e $(printf '%q' $mystr)
output: say \"hi\"
回答6:
Store the double quote character as variable:
dqt='"'
echo "Double quotes ${dqt}X${dqt} inside a double quoted string"
Output:
Double quotes "X" inside a double quoted string
回答7:
Make use of $"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"
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.
回答8:
add "\" before double quote to escape it, instead of \
#! /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
来源:https://stackoverflow.com/questions/3834839/how-to-escape-a-double-quote-inside-double-quotes