关于命令行中带双引号的回车解释的困惑

huangapple go评论86阅读模式
英文:

Confusion about Newline Interpretation in Command Line Echo with Double Quotes

问题

I've been learning about command line expansions and substitution, including path name expansion and command substitution. In my studies, I came across the concept of controlling these expansions using double quotes. While experimenting with these concepts, I encountered a behavior that I'm having trouble interpreting.

When I use the command echo $(cal), the output seems to ignore the newlines and is displayed as a single line:

August 2023 Su Mo Tu We Th Fr Sa 1 2 3 4 ...

However, when I use double quotes like this: echo "$(cal)", the output is correctly formatted with newlines:

    August 2023
Su Mo Tu We Th Fr Sa
      1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

From my understanding, using double quotes appears to render the newline characters (\n) correctly in the echo "$(cal)" command.

However, what confuses me is that when I try a similar approach with other characters, such as A\nB, using the command echo "A\nB", the output does not interpret the newline character and instead displays it as literal text:

A\nB

I discovered that I need to use the -e switch, like this: echo -e "A\nB", to achieve the expected behavior:

A
B

Could someone please explain why the behavior differs between these two cases? Why does echo "$(cal)" correctly interpret newline characters within double quotes, while echo "A\nB" requires the -e switch for the correct interpretation?

I appreciate any insights or clarifications you can provide. Thank you!

英文:

I've been learning about command line expansions and substitution, including path name expansion and command substitution. In my studies, I came across the concept of controlling these expansions using double quotes. While experimenting with these concepts, I encountered a behavior that I'm having trouble interpreting.

When I use the command echo $(cal), the output seems to ignore the newlines and is displayed as a single line:

August 2023 Su Mo Tu We Th Fr Sa 1 2 3 4 ...

However, when I use double quotes like this: echo "$(cal)", the output is correctly formatted with newlines:

    August 2023
Su Mo Tu We Th Fr Sa
      1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

From my understanding, using double quotes appears to render the newline characters (\n) correctly in the echo "$(cal)" command.

However, what confuses me is that when I try a similar approach with other characters, such as A\nB, using the command echo "A\nB", the output does not interpret the newline character and instead displays it as literal text:

A\nB

I discovered that I need to use the -e switch, like this: echo -e "A\nB", to achieve the expected behavior:

A
B

Could someone please explain why the behavior differs between these two cases? Why does echo "$(cal)" correctly interpret newline characters within double quotes, while echo "A\nB" requires the -e switch for the correct interpretation?

I appreciate any insights or clarifications you can provide. Thank you!

答案1

得分: 1

在`"A\nB"`中没有换行字符。 C风格的转义序列不会在单引号或双引号内解释。反斜杠可以在双引号字符串中用于转义特殊字符,如`$`,以使它们被字面解释,但`\n`没有特殊含义,会被字面解释。

要使用C风格的转义序列创建字符串,请使用`$'string'`。

echo $'A\nB'

将打印

A
B


`-e`选项告诉`echo`命令在打印字符串时解释字符串内的转义序列。
英文:

There are no newline characters in "A\nB". C-style escape sequences aren't interpreted inside single quotes or double quotes. Backslash can be used in double-quoted strings to escape special characters like $ so they're treated literally, but \n has no special meaning and is interpreted literally.

To make a string using C-style escape sequences, use $'string'.

echo $'A\nB'

will print

A
B

The -e option to echo tells the echo command to interpret escape sequences inside the string when it's printing.

huangapple
  • 本文由 发表于 2023年8月11日 04:40:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76879186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定