This
STR="Hello\nWorld"
echo $STR
作为输出产生
Hello\nWorld
而不是
Hello
World
我应该做什么有一个换行在字符串?
注意:此问题与echo无关。 我知道echo -e,但我正在寻找一种解决方案,允许传递一个字符串(其中包括换行符)作为参数到其他命令,没有类似的选项来解释\n的换行符。
This
STR="Hello\nWorld"
echo $STR
作为输出产生
Hello\nWorld
而不是
Hello
World
我应该做什么有一个换行在字符串?
注意:此问题与echo无关。 我知道echo -e,但我正在寻找一种解决方案,允许传递一个字符串(其中包括换行符)作为参数到其他命令,没有类似的选项来解释\n的换行符。
当前回答
我发现-e标志优雅而直接
bash$ STR="Hello\nWorld"
bash$ echo -e $STR
Hello
World
如果字符串是另一个命令的输出,我只使用引号
indexes_diff=$(git diff index.yaml)
echo "$indexes_diff"
其他回答
这并不理想,但我已经编写了大量代码,并以类似于问题中使用的方法定义了字符串。被接受的解决方案要求我重构大量代码,因此,我将每个\n替换为“$'\n'”,这对我来说很有效。
如果你正在使用Bash,你可以在一个特别引用的$'string'中使用反斜杠转义。例如,添加\n:
STR=$'Hello\nWorld'
echo "$STR" # quotes are required here!
打印:
Hello
World
如果你使用的是其他shell,只需在字符串中插入换行符:
STR='Hello
World'
Bash在$ "字符串中识别出许多其他反斜杠转义序列。以下是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:
\a alert (bell)
\b backspace
\e
\E an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\" double quote
\nnn the eight-bit character whose value is the octal value
nnn (one to three digits)
\xHH the eight-bit character whose value is the hexadecimal
value HH (one or two hex digits)
\cx a control-x character
The expanded result is single-quoted, as if the dollar sign had not
been present.
A double-quoted string preceded by a dollar sign ($"string") will cause
the string to be translated according to the current locale. If the
current locale is C or POSIX, the dollar sign is ignored. If the
string is translated and replaced, the replacement is double-quoted.
那些只需要换行符而不喜欢破坏缩进的多行代码的挑剔的人可以这样做:
IFS="$(printf '\nx')"
IFS="${IFS%x}"
Bash(可能还有其他shell)在命令替换之后会占用所有尾随的换行符,因此需要用非换行符结束printf字符串,然后删除它。这也可以很容易地成为一个联机程序。
IFS="$(printf '\nx')" IFS="${IFS%x}"
我知道这是两个动作而不是一个,但我的缩进和可移植性强迫症现在是和平的:)我最初开发这个是为了能够分割换行分离的输出,我最终使用了一个修改,使用\r作为结束字符。这使得换行分割甚至适用于以\r\n结尾的dos输出。 IFS = " $ (printf ' r \ n \ ')”
唯一简单的替代方法是在变量中输入新行: STR = '新的美元 线的 $STR" %s " 新 行
是的,这意味着在代码中需要的地方输入Enter。
有几个新行字符的等价物。 一种常用的表示新行字符的方法。 新行字符的八进制值。 新行字符的十六进制值。
但所有这些都需要一些工具(POSIX printf)的“解释”:
echo -e "new\nline" ### on POSIX echo, `-e` is not required.
printf 'new\nline' ### Understood by POSIX printf.
printf 'new\012line' ### Valid in POSIX printf.
printf 'new\x0Aline'
printf '%b' 'new\0012line' ### Valid in POSIX printf.
因此,这个工具需要用new-line来构建一个字符串:
$ STR="$(printf 'new\nline')"
$ printf '%s' "$STR"
new
line
在某些shell中,序列$'是一个特殊的shell展开。 已知在ksh93, bash和zsh中工作: STR =美元“新\在线” 当然,也可能有更复杂的解决方案: $ echo '6e65770a6c696e650a' | XXD -p -r 新 行
Or
$ echo "new line" | sed 's/ \+/\n/g'
new
line
问题不在于外壳。问题实际上出在echo命令本身,以及变量插值时缺少双引号。您可以尝试使用echo -e,但并非所有平台都支持它,这也是现在推荐使用printf的原因之一,因为它具有可移植性。
您还可以尝试直接在shell脚本中插入换行符(如果您正在编写脚本),因此它看起来像…
#!/bin/sh
echo "Hello
World"
#EOF
或者同样的
#!/bin/sh
string="Hello
World"
echo "$string" # note double quotes!