在 OSX bash 中打印回车
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10670610/
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
print carriage return in OSX bash
提问by tony19
Why does echo-ing a carriage return from OSX Terminal behave differently than from a bashscript?
为什么echo从 OSX 终端回车与从bash脚本回车的行为不同?
From Terminal in OSX 10.7.3:
从 OSX 10.7.3 中的终端:
$ echo $SHELL
/bin/bash
$ /bin/bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.
$ echo -ne "hello\rbye"
byelo
But I see a different result from test.sh:
但我看到了不同的结果test.sh:
#!/bin/bash
echo -ne "hello\rbye"
...running test.shgives me:
...跑步test.sh给了我:
$ ./test.sh
byehello
I was expecting byelo. Why is it different? and how do I correct this?
我很期待byelo。为什么不一样?我该如何纠正?
采纳答案by tony19
It had something to do with #!/bin/shat the top of my script. After I changed it to #!/bin/bash, I saw the expected output.
它与#!/bin/sh我的脚本顶部有关。将其更改为 后#!/bin/bash,我看到了预期的输出。
回答by David W.
I just ran the same thing on my Mac, and got the same results.
我只是在我的 Mac 上运行了同样的事情,并得到了相同的结果。
I'm thinking two possibilities:
我在想两种可能:
- One of your
set -oorshootsettings might be doing this - Your
.bashrc(which would be called when you run a shell script) is doing something.
- 您的一项
set -o或shoot多项设置可能正在执行此操作 - 您的
.bashrc(在运行 shell 脚本时会被调用)正在做某事。
My results look like this:
我的结果是这样的:
$ echo -ne "hello\rbye"
bye$
$ test.sh #Shell script with the one line in it
buy$ []
The []represents the cursor. I have $PS1="$ ".
的[]表示光标。我有$PS1="$ "。
A suggesting, use printfif you want to do things like this.
一个建议,printf如果你想做这样的事情,请使用。
$ printf "hello\rbye"
printfdoesn't automatically add a CR line, and you don't have to give it any special options.
printf不会自动添加 CR 行,并且您不必为其提供任何特殊选项。

