Bash允许您将字符串相邻放置,最终它们将被粘在一起。
所以这:
$ echo "Hello"', world!'
产生
Hello, world!
诀窍是根据需要在单引号和双引号字符串之间交替。不幸的是,它很快变得非常混乱。例如:
$ echo "I like to use" '"double quotes"' "sometimes"
产生
I like to use "double quotes" sometimes
在您的示例中,我将执行以下操作:
$ 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
产生以下输出:
load data local infile "'gfpoint.csv'" into table example FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "'\n'" IGNORE 1 LINES
很难看到这里发生了什么,但是我可以使用Unicode引号对其进行注释。以下内容无法在bash中使用-只是出于说明目的:
dbload=
' load data local infile "
' 'gfpoint.csv'
' " into
' table $dbtable FIELDS TERMINATED BY ',' ENCLOSED BY '
' "
' ' LINES
' TERMINATED BY "
' '\n'
' " IGNORE 1 LINES
' ' ' ' ' ' ' ' '
上面的诸如“''”的引号将由bash解释。像这样的引号" '
将最终出现在结果变量中。
如果我对前面的示例进行相同的处理,则如下所示:
$ echo
“ I like to use
”
“ "double quotes"
”
“ sometimes
”