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
.
Use a backslash:
check out printf...
Without using printf
output: say "hi"
Using printf
output: say \"hi\"
Bash allows you to place strings adjacently, and they'll just end up being glued together.
So this:
produces
The trick is to alternate between single and double-quoted strings as required. Unfortunately, it quickly gets very messy. For example:
produces
In your example, I would do it something like this:
which produces the following output:
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
”add
"\"
before double quote to escape it, instead of\
Make use of $"string".
In this example, it would be,
Note(from the man page):
Store the double quote character as variable:
Output: