Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I escape a double quote inside double quotes?

Tags:

bash

quotes

How can I escape double quotes 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 to escape correctly. I can't use single quotes for my variable, because I want to use variable $dbtable.

like image 558
Sean Nguyen Avatar asked Sep 30 '10 21:09

Sean Nguyen


People also ask

How do you use double quotes inside a double quote?

Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash.

How do you escape a double quote?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

How do you escape a quote?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string. Here is an example. The backslashes protect the quotes, but are not printed.

How do you echo a quote with a string?

Simply type echo 'text "hey"' - wrap the entire string in single quotes (').


1 Answers

Use a backslash:

echo "\""     # Prints one " character. 
like image 144
Peter Avatar answered Nov 30 '22 02:11

Peter