如何在双引号内的转义双引号中转义双引号?

时间:2019-01-07 10:57:29

标签: bash escaping quotes double-quotes

我尝试执行以下命令:

su -l user1 -c "hive -e \"ALTER TABLE schema1.table1 DROP IF EXISTS PARTITION (att1=\"$val\");\""

我收到错误消息:

FAILED: ParseException line 1:81 cannot recognize input near 'val1' ')' '<EOF>' in constant

所以这里的问题是由于第二级嵌套的双引号引起的,在它们之间有一个变量$val,其值为val1

您能帮我解决吗?最好的办法是给我一个嵌套引号的规则。

1 个答案:

答案 0 :(得分:0)

In this particular case, you can use single quotes instead of the internal (currently escaped) double quotes, since single quotes inside double quotes are not special at all.

su -l user1 -c "hive -e 'ALTER TABLE schema1.table1 DROP IF EXISTS PARTITION (att1=\"$val\");'"

In the general case, you can't always avoid death by backslash escapes. The general quoting rules are very straightforward; text between single quotes is quoted verbatim, whereas double quotes are weaker but allow you to backslash things which should not be touched by the shell.