How can I escape a double quote inside double quotes?

后端 未结 8 1504
庸人自扰
庸人自扰 2020-11-22 15:56

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 \\\"\'gfp         


        
相关标签:
8条回答
  • 2020-11-22 16:47

    A simple example of escaping quotes in the shell:

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

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

    Alternatively:

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

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

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

    0 讨论(0)
  • 2020-11-22 16:49

    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.

    0 讨论(0)
提交回复
热议问题