bash 如何让 echo 解释反斜杠转义而不打印尾随换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10576571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make echo interpret backslash escapes and not print a trailing newline?
提问by seanwatson
I would like to use echo
in bash to print out a string of characters followed by only a carriage return. I've looked through the man page and have found that echo -e
will make echo
interpret backslash escape characters. Using that I can say echo -e 'hello\r'
and it will print like this
我想echo
在 bash 中使用打印出一串字符,后跟一个回车符。我浏览了手册页,发现echo -e
可以echo
解释反斜杠转义字符。使用它我可以说它echo -e 'hello\r'
会像这样打印
$>echo -e 'hello\r'
hello
$>
So it looks like it handled the carriage return properly. I also found echo -n
in the man page will stop echo
from inserting a newline character and it looks like it works when I do this
所以看起来它正确处理了回车。我还在echo -n
手册页中发现将停止echo
插入换行符,当我这样做时它看起来像它
$>echo -n 'hello\r'
hello\r$>
The problem I'm having is in combining both -e
and -n
. I've tried each of echo -e -n 'hello\r'
, echo -n -e 'hello\r'
, echo -en 'hello\r'
, and echo -ne 'hello\r'
and nothing gets printed like so:
我遇到的问题是将-e
和结合起来-n
。我已经尝试了每个echo -e -n 'hello\r'
, echo -n -e 'hello\r'
, echo -en 'hello\r'
, 和echo -ne 'hello\r'
并且没有像这样打印:
$>echo -ne 'hello\r'
$>
Is there something I'm missing here or can the -e
and -n
options not be used together?
有什么我在这里遗漏的吗?-e
和-n
选项不能一起使用吗?
回答by Mark Reed
I think it's working, you're just not seeing it. This is your command:
我认为它有效,只是你没有看到。这是你的命令:
$> echo -ne 'hello\r'
Because of the carriage return (\r
), that will leave the cursor at the start of the same line on the terminal where it wrote the hello
- which means that's where the next thing output to the terminal will be written. So if your actual prompt is longer than the $>
you show here, it will overwrite the hello
completely.
由于回车 ( \r
),这会将光标留在终端上写入该行的同一行的开头hello
- 这意味着将写入到终端的下一个输出内容的位置。因此,如果您的实际提示比$>
您在此处显示的要长,它将hello
完全覆盖。
This sequence will let you see what's actually happening:
此序列将让您看到实际发生的情况:
echo -ne 'hello\r'; sleep 5; echo 'good-bye'
But for better portability to other shells, I would avoid using options on echo
like that. Those are purely bashisms, not supported by the POSIX standard. The printf
builtin, however, is specified by POSIX. So if you want to display strings with no newline and parsing of backslash sequences, you can just use it:
但是为了更好地移植到其他 shell,我会避免使用echo
类似的选项。这些纯粹是 bashisms,不受 POSIX 标准支持。该printf
内建,然而,由POSIX指定。所以如果你想显示没有换行符的字符串和反斜杠序列的解析,你可以使用它:
printf '%s\r' 'hello'
回答by Keith Thompson
There are numerous different implementations of the echo
command. There's one built into most shells (with different behavior for each), and the behavior of /bin/echo
varies considerably from one system to another.
该echo
命令有多种不同的实现方式。大多数外壳都内置了一个(每个外壳具有不同的行为),并且/bin/echo
从一个系统到另一个系统的行为差异很大。
Rather than echo
, use printf
. It's built into bash as well as being available as an external command, and its behavior is much more consistent across implementations. (The major variation is that the GNU coreutils printf
recognizes --help
and --version
options.)
而不是echo
,使用printf
. 它内置于 bash 中,并且可以作为外部命令使用,并且它的行为在各个实现中更加一致。(主要的变化是 GNU coreutilsprintf
识别--help
和--version
选择。)
Just use:
只需使用:
printf 'hello\r'
回答by David W.
I'd like you to introduce you to printf.
我想让你向你介绍printf。
OP, meet printf
. printf
. This is the OP...
欧,见面printf
。printf
. 这是OP...
Whenever you are trying to do anything unusualwith output in BASH, you should switch to printf
. In fact, I use printf
all the time, so my scripts will run under both BASH and Kornshell.
每当您尝试对BASH 中的输出执行任何不寻常的操作时,您都应该切换到printf
. 事实上,我printf
一直在使用,所以我的脚本将在 BASH 和 Kornshell 下运行。
Although BASH and Kornshell are 99% the same, the echo
command is different. In Kornshell, it's deprecated, and you're supposed to use the builtin print
. However, there's no print
in BASH. Using printf
solves the problem because it works (sort of) the same in both shells.
虽然 BASH 和 Kornshell 99% 相同,但echo
命令不同。在 Kornshell 中,它已被弃用,您应该使用内置print
. 但是,print
BASH 中没有。使用printf
解决了这个问题,因为它在两个 shell 中工作(有点)相同。
Since printf
doesn't automatically end each line with a \n
, you don't have to worry about how to prevent the \n
from being appended. Heck, if you want a \n
, you have to put it yourself. In your case:
由于printf
不会自动以 a 结束每一行\n
,因此您不必担心如何防止\n
附加 。哎呀,如果你想要一个\n
,你必须自己把它放进去。在你的情况下:
printf `hello\r`
However, for safety reasons, you should really do this:
但是,出于安全原因,您确实应该这样做:
printf '%s\r' 'hello'
This is a habit I've gotten into when I'm using printf
. Imagine if I want to print out $FOO
, and I do this:
这是我在使用printf
. 想象一下,如果我想打印出来$FOO
,我会这样做:
printf "$FOO\n"
That will normally work, but what if $FOO
has a dash at the beginning, or has a %
sign in it. The above won't do what I want to do. However, if I do this:
这通常会起作用,但是如果$FOO
开头有一个破折号,或者里面有一个%
标志呢?以上不会做我想做的事情。但是,如果我这样做:
printf '%s\n' "$FOO"
This will print out $FOO
no matter what $FOO
has in it.
$FOO
无论里面有什么,这都会打印出来$FOO
。
回答by Paused until further notice.
Quoting. It's the wave of the future. Always quote.
引用。这是未来的潮流。总是引用。
echo -ne 'hello\r'
Use double quotes if you put a variable inside that you want expanded.
如果将要扩展的变量放入其中,请使用双引号。