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的换行符。
当前回答
那些只需要换行符而不喜欢破坏缩进的多行代码的挑剔的人可以这样做:
IFS="$(printf '\nx')"
IFS="${IFS%x}"
Bash(可能还有其他shell)在命令替换之后会占用所有尾随的换行符,因此需要用非换行符结束printf字符串,然后删除它。这也可以很容易地成为一个联机程序。
IFS="$(printf '\nx')" IFS="${IFS%x}"
我知道这是两个动作而不是一个,但我的缩进和可移植性强迫症现在是和平的:)我最初开发这个是为了能够分割换行分离的输出,我最终使用了一个修改,使用\r作为结束字符。这使得换行分割甚至适用于以\r\n结尾的dos输出。 IFS = " $ (printf ' r \ n \ ')”
其他回答
我不是bash专家,但这个对我来说很管用:
STR1="Hello"
STR2="World"
NEWSTR=$(cat << EOF
$STR1
$STR2
EOF
)
echo "$NEWSTR"
我发现这更容易格式化文本。
如果你正在使用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.
这并不理想,但我已经编写了大量代码,并以类似于问题中使用的方法定义了字符串。被接受的解决方案要求我重构大量代码,因此,我将每个\n替换为“$'\n'”,这对我来说很有效。
那些只需要换行符而不喜欢破坏缩进的多行代码的挑剔的人可以这样做:
IFS="$(printf '\nx')"
IFS="${IFS%x}"
Bash(可能还有其他shell)在命令替换之后会占用所有尾随的换行符,因此需要用非换行符结束printf字符串,然后删除它。这也可以很容易地成为一个联机程序。
IFS="$(printf '\nx')" IFS="${IFS%x}"
我知道这是两个动作而不是一个,但我的缩进和可移植性强迫症现在是和平的:)我最初开发这个是为了能够分割换行分离的输出,我最终使用了一个修改,使用\r作为结束字符。这使得换行分割甚至适用于以\r\n结尾的dos输出。 IFS = " $ (printf ' r \ n \ ')”
Echo是90年代的,而且充满了危险,使用它应该会导致内核转储不少于4GB。严肃地说,echo的问题是Unix标准化进程最终发明printf实用程序的原因,它解决了所有问题。
要在字符串中获取换行符,有两种方法:
# 1) Literal newline in an assignment.
FOO="hello
world"
# 2) Command substitution.
BAR=$(printf "hello\nworld\n") # Alternative; note: final newline is deleted
printf '<%s>\n' "$FOO"
printf '<%s>\n' "$BAR"
在那里!没有SYSV和BSD的回声疯狂,所有东西都被整齐地打印出来,并完全支持C转义序列。请大家现在就使用printf来满足所有的输出需求,不要回头。