这个
STR="Hello\nWorld"
echo $STR
Run Code Online (Sandbox Code Playgroud)
产生输出
Hello\nWorld
Run Code Online (Sandbox Code Playgroud)
代替
Hello
World
Run Code Online (Sandbox Code Playgroud)
如何在字符串中添加换行符?
注意:这个问题不是关于回声.
我知道echo -e
,但我正在寻找一种解决方案,允许将一个字符串(包括换行符)作为参数传递给其他命令,这些命令没有类似的选项来解释\n
为换行符.
amp*_*ine 316
解决方案是使用$'string'
,例如:
$ STR=$'Hello\nWorld'
$ echo "$STR"
Hello
World
Run Code Online (Sandbox Code Playgroud)
以下是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.
Run Code Online (Sandbox Code Playgroud)
Jen*_*ens 154
Echo是九十年代,充满了危险,它的使用应该导致核心转储不低于4GB.说真的,echo的问题是Unix标准化过程最终发明printf
实用程序的原因,消除了所有问题.
所以要在字符串中获取换行符:
FOO="hello
world"
BAR=$(printf "hello\nworld\n") # Alternative; note: final newline is deleted
printf '<%s>\n' "$FOO"
printf '<%s>\n' "$BAR"
Run Code Online (Sandbox Code Playgroud)
那里!没有SYSV与BSD的回声疯狂,一切都得到了整齐的打印和完全便携式支持C转义序列.大家请printf
现在使用,永远不要回头.
zve*_*zda 54
我根据其他答案做了什么
NEWLINE=$'\n'
my_var="__between eggs and bacon__"
echo "spam${NEWLINE}eggs${my_var}bacon${NEWLINE}knight"
# which outputs:
spam
eggs__between eggs and bacon__bacon
knight
Run Code Online (Sandbox Code Playgroud)
Pac*_*ace 29
问题不在于shell.问题实际上是echo
命令本身,以及变量插值周围缺少双引号.您可以尝试使用echo -e
但并非在所有平台上都支持,printf
现在建议使用其中一个原因来实现可移植性.
您也可以尝试将换行符直接插入到shell脚本中(如果脚本是您正在编写的脚本),那么它看起来像......
#!/bin/sh
echo "Hello
World"
#EOF
Run Code Online (Sandbox Code Playgroud)
或者等价的
#!/bin/sh
string="Hello
World"
echo "$string" # note double quotes!
Run Code Online (Sandbox Code Playgroud)
小智 21
唯一简单的替代方法是在变量中实际输入一个新行:
$ STR='new
line'
$ printf '%s' "$STR"
new
line
Run Code Online (Sandbox Code Playgroud)
是的,这意味着Enter在代码中需要编写代码.
new line
角色有几个等价物.
\n ### A common way to represent a new line character.
\012 ### Octal value of a new line character.
\x0A ### Hexadecimal value of a new line character.
Run Code Online (Sandbox Code Playgroud)
但所有这些都需要一些工具(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.
Run Code Online (Sandbox Code Playgroud)
因此,该工具需要使用换行符构建一个字符串:
$ STR="$(printf 'new\nline')"
$ printf '%s' "$STR"
new
line
Run Code Online (Sandbox Code Playgroud)在某些shell中,序列$'是一个特殊的shell扩展.已知可以在ksh93,bash和zsh中工作:
$ STR=$'new\nline'
Run Code Online (Sandbox Code Playgroud)当然,更复杂的解决方案也是可能的:
$ echo '6e65770a6c696e650a' | xxd -p -r
new
line
Run Code Online (Sandbox Code Playgroud)
要么
$ echo "new line" | sed 's/ \+/\n/g'
new
line
Run Code Online (Sandbox Code Playgroud)Sim*_*nda 19
我发现-e
国旗优雅而直截了当
-e
#outputs-e
如果字符串是另一个命令的输出,我只使用引号
-e
单引号前面的$如下所示,但是双引号无效。
$ echo $'Hello\nWorld'
Hello
World
$ echo $"Hello\nWorld"
Hello\nWorld
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
267143 次 |
最近记录: |