当我做
str="Hello World\n===========\n"
Run Code Online (Sandbox Code Playgroud)
我\n也打印出来了 那我怎么会有换行符呢?
enz*_*tib 119
在bash你可以使用语法
str=$'Hello World\n===========\n'
Run Code Online (Sandbox Code Playgroud)
以 a 开头的单引号$是一种允许在字符串中插入转义序列的新语法。
还printf内置允许将结果输出保存到变量
printf -v str 'Hello World\n===========\n'
Run Code Online (Sandbox Code Playgroud)
这两种解决方案都不需要子shell。
如果在下面需要打印字符串,则应使用双引号,如下例所示:
echo "$str"
Run Code Online (Sandbox Code Playgroud)
因为当您打印不带引号的字符串时,换行符将转换为空格。
Gil*_*il' 38
您可以在单引号内放置文字换行符(在任何 Bourne/POSIX 样式的 shell 中)。
str='Hello World
===========
'
Run Code Online (Sandbox Code Playgroud)
对于多行字符串,这里的文档通常很方便。该字符串作为命令的输入提供。
mycommand <<'EOF'
Hello World
===========
EOF
Run Code Online (Sandbox Code Playgroud)
如果要将字符串存储在变量中,请cat在命令替换中使用该命令。字符串末尾的换行符将被命令替换删除。如果您想保留最后的换行符,请在末尾放置一个塞子,然后将其剥离。在符合 POSIX 的 shell 中,您可以编写str=$(cat <<'EOF'); str=${str%a}后跟适当的 heredoc,但 bash 要求 heredoc 出现在右括号之前。
str=$(cat <<'EOF'
Hello World
===========
a
EOF
); str=${str%a}
Run Code Online (Sandbox Code Playgroud)
在 ksh、bash 和 zsh 中,您可以使用带$'…'引号的形式在引号内展开反斜杠转义。
str=$'Hello World\n===========\n'
Run Code Online (Sandbox Code Playgroud)
pih*_*agy 14
如果您的脚本中多次需要换行符,您可以声明一个包含换行符的全局变量。这样你就可以在双引号字符串(变量扩展)中使用它。
NL=$'\n'
str="Hello World${NL} and here is a variable $PATH ===========${NL}"
Run Code Online (Sandbox Code Playgroud)
Mik*_*ike 10
你在使用“回声”吗?试试“echo -e”。
echo -e "Hello World\n===========\n"
Run Code Online (Sandbox Code Playgroud)
为了补充现有的优秀答案:
如果您正在使用bash并且更喜欢使用实际的换行符来提高可读性,read则是在变量中捕获 here-doc 的另一种选择,它(与此处的其他解决方案一样)不需要使用子shell。
# Reads a here-doc, trimming leading and trailing whitespace.
# Use `IFS= read ...` to preserve it (the trailing \n, here).
read -r -d '' str <<'EOF' # Use `IFS= read ...` to preserve the trailing \n
Hello World
===========
EOF
Run Code Online (Sandbox Code Playgroud)
# Test: output the variable enclosed in "[...]", to show the value's boundaries.
$ echo "$str"
[Hello World
===========]
Run Code Online (Sandbox Code Playgroud)
-r确保read不解释输入(默认情况下,它会特殊对待反斜杠,但很少需要)。
-d ''将“记录”分隔符设置为空字符串,导致一次read读取整个输入(而不是单行)。
请注意,通过将$IFS(内部字段分隔符)保留为默认值$' \t\n'(空格、制表符、换行符),任何前导和尾随空格都将从分配给 的值中删除$str,其中包括 here-doc 的尾随换行符。
(请注意,即使 here-doc 的正文从开始分隔符(此处)之后的行开始'EOF',它也不包含前导换行符)。
通常,这是所需的行为,但如果您确实想要该尾随换行符,请使用IFS= read -r -d ''而不仅仅是read -r -d '',但请注意,任何前导和尾随空格都会被保留。
(请注意,IFS= 直接在read命令前添加意味着赋值仅在该命令期间有效,因此无需恢复先前的值。)
使用 here-doc 还允许您选择使用缩进来设置多行字符串以提高可读性:
# Caveat: indentation must be actual *tab* characters - spaces won't work.
read -r -d '' str <<-'EOF' # NOTE: Only works if the indentation uses actual tab (\t) chars.
Hello World
===========
EOF
Run Code Online (Sandbox Code Playgroud)
# Output the variable enclosed in "[...]", to show the value's boundaries.
# Note how the leading tabs were stripped.
$ echo "$str"
[Hello World
===========]
Run Code Online (Sandbox Code Playgroud)
-在<<here-doc 分隔符 ( 'EOF', here )之间放置和开始分隔符会导致前导制表符从 here-doc 正文甚至结束分隔符中剥离,但请注意,这仅适用于实际制表符,而不适用于空格,因此如果您编辑器将 Tab 键转换为空格,需要额外的工作。
小智 5
从所有讨论中,这是对我来说最简单的方法:
bash$ str="Hello World
==========="
bash$ echo "$str"
Hello World
===========
Run Code Online (Sandbox Code Playgroud)
该回声命令必须使用双引号。