什么$'\n'在bash中意味着什么?

2 unix linux bash shell

这个stackoverflow问题的答案在Linux shell中,我如何处理多行字符串的每一行?提到这$'\n'是一种特殊的语法"在每个shell中都不可用".

我想知道语法是什么意思?

谢谢.

lar*_*sks 5

请参见手册页的QUOTING部分bash:

   Words of the form $'string' are treated specially.  The word  expands  to
   string,  with  backslash-escaped  characters replaced as specified by the
   ANSI C standard.  Backslash escape sequences, if present, are decoded  as
   follows:

...
              \n     new line
...

   The  expanded result is single-quoted, as if the dollar sign had not been
   present.
Run Code Online (Sandbox Code Playgroud)

此语法不是标准Unix Bourne shell的一部分.它可能是特定于bash,虽然我没有环顾四周看其他shell是否支持它.

  • @tripleee `$'` 字符串实际上并不是真正的“bashism”:zsh 支持它,busybox ash 支持它,我现在无法验证,但我认为一些 ksh 版本支持它,并且该语法实际上起源于很久以前:它来得太晚了,无法得到适当的标准化,但显然很多 shell 都支持它。 (2认同)