[英]echo newline character output
I'm writing a bash script and when I write the line: 我正在编写bash脚本,并且在编写该行时:
echo "l=l.split('\n')"
I would like the output to actually be l=l.split('\\n')
but get: 我希望输出实际上是
l=l.split('\\n')
但得到:
l=l.split(' ')
Any idea on how to fix this? 关于如何解决此问题的任何想法? I tried using quotations at different spots and escaping the characters differently but nothing seems to be working.
我尝试在不同位置使用引号,并以不同的方式转义字符,但似乎没有任何效果。 Appreciate the help!
感谢帮助!
**Worth noting - if I simply type the echo command into the terminal I get my desired output.. Not sure why a script is treated differently. **值得一提-如果我只是在终端中键入echo命令,我将得到所需的输出。。不确定为什么要对脚本进行不同的处理。
It sounds like perhaps you got the shebang wrong (for a bash
script, anyway). 听起来好像您把shebang弄错了(无论如何,对于
bash
脚本)。 Take for example: 举个例子:
$ cat test.sh
#!/bin/sh
echo "l=l.split('\n')"
$ ./test.sh
l=l.split('
')
$ cat test.bash
#!/bin/bash
echo "l=l.split('\n')"
$ ./test.bash
l=l.split('\n')
Even though bash
and sh
may be provided by the same shell on some systems, there are subtle differences in their behavior. 即使在某些系统上
bash
和sh
可以由同一shell提供,它们的行为也存在细微的差异。 If you want it to behave like it does for you in a terminal, be sure to use #!/bin/bash
. 如果您希望它的行为像在终端机中一样,请确保使用
#!/bin/bash
。
tl;dr TL;博士
If your script is really being run by bash
, then something must have turned on the off-by-default xpg_echo
shell option - either directly, or indirectly via shell option posix
. 如果您的脚本实际上是由
bash
运行的 ,则必须打开默认关闭的xpg_echo
shell选项-直接或通过shell选项posix
xpg_echo
。
posix
is implicitly turned on when Bash is invoked as sh
( /bin/sh
), which happens on macOS, for instance. 当Bash作为
sh
( /bin/sh
)调用时, posix
是隐式打开的,例如在macOS上。
If your script is not being run by bash
, which is the likeliest explanation, as suggested in FatalError's helpful answer : 如果您的脚本不是由
bash
运行 ,这是最可能的解释,如FatalError的有用答案中所建议:
Ensure that your script's shebang line is either 确保脚本的shebang行是
#!/bin/bash
or #!/usr/bin/env bash
. #!/bin/bash
或#!/usr/bin/env bash
。
Alternatively, pass it directly to bash
: bash <script>
或者,将其直接传递给
bash
: bash <script>
The portable solution , which shields you from variable echo
behavior, is to use printf
as follows: 可移植的解决方案可以使您免受可变的
echo
行为的影响,它可以按以下方式使用printf
:
printf '%s\\n' "l=l.split('\\n')"
In bash
, the interpretation of escape sequences such as \\n
by echo
is turned OFF by default. 在
bash
,默认情况下将关闭echo
对转义序列(例如\\n
的解释。
Option xpg_echo
must be turned on for escape sequences to be recognized by echo
. 必须打开选项
xpg_echo
才能使echo
识别转义序列 。
This option is implicitly on if you run bash
in POSIX compatibility mode (verify with shopt -o posix
), which also happens if you run Bash as sh
, as is the case on macOS , for instance, where /bin/sh
is Bash. 如果以POSIX兼容模式运行
bash
(使用shopt -o posix
验证), 则该选项隐式启用,如果 以sh
身份运行Bash ,例如在macOS上 ,例如/bin/sh
是Bash,则也会发生这种情况。
Note that irrespective of the state of xpg_echo
by itself, you can opt into escape-sequence interpretation ad-hoc with echo -e
and opt out with echo -E
. 请注意,无论
xpg_echo
本身是什么状态, 都可以使用echo -e
临时选择转义序列解释,并使用echo -E
选择退出 。
However, this does not work when running in POSIX compatibility mode ( shopt -o posix
) , where - in compliance with POSIX - echo
supports no options at all . 然而, 在POSIX兼容模式(运行时,这不起作用
shopt -o posix
符合POSIX - - ),其中echo
支持没有任何选项 。
In other words: The following would only work if (a) your script is really being executed by bash
and (b) option posix
is off : 换句话说:仅当(a)
bash
确实在执行您的脚本并且(b) posix
选项为off时,以下内容才有效 :
echo -E "l=l.split('\n')"
Again, printf
is the portable , POSIX-compliant alternative that works consistently across all POSIX-compatible shells, irrespective of the state of shell options : 同样,
printf
是可移植的 ,与POSIX兼容的替代方案 , 可在所有与POSIX兼容的shell中一致地工作,而与shell选项的状态无关 :
# '%s\n': *without* escape-sequence interpretation
$ printf '%s\n' "l=l.split('\n')"
l=l.split('\n')
# '%b\n': *with* escape-sequence interpretation
$ printf '%b\n' "l=l.split('\n')"
l=l.split('
')
这样就解决了问题。
echo "l=l.split('\\\n')"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.