问题描述
在bash中,在这种特定情况下,echo的行为如下:
In bash, in this specific case, echo behaves like so:
$ bash -c 'echo "a\nb"'
a\nb
但是在zsh中,同一件事却大不相同...:
but in zsh the same thing turns out very differently...:
$ zsh -c 'echo "a\nb"'
a
b
然后在鱼中wi游,因为我很好奇:
and fwiw in fish, because I was curious:
$ fish -c 'echo "a\nb"'
a\nb
我确实意识到自己可以跑步:
I did realize that I can run:
$ zsh -c 'echo -E "a\nb"'
a\nb
但是现在我担心我会因这种基本操作而陷入更多陷阱.(因此,我对鱼类的研究:如果我必须对zsh进行如此低的修改,为什么不一直坚持下去,转而采取一种公然认为与众不同的方式?)
But now I am worried that I'm about to stumble into more gotchas on such a basic operation. (Thus my investigation into fish: if I'm going to have to make changes at such a low level for zsh, why not go all the way and switch up to something that is blatant about being so different?)
我自己没有找到任何文档来帮助澄清bash vs zsh或直接列出差异的页面中的回声差异,所以这里有人可以将它们列出来吗?也许在进行切换时将我引导到任何可能产生影响的更广泛的陷阱,就可以解决这种情况?
I did not myself find any documentation to help clarify this echo difference in bash vs zsh or pages directly listing the differences, so can someone here list them out? And maybe direct me to any broader set of potentially impactful gotchas when making the switch, that would cover this case?
推荐答案
通常希望使用 printf
以获得一致的结果.
Usually prefer printf
for consistent results.
如果需要可预测的一致的 echo
实现,则可以使用自己的函数覆盖它.
If you need predictable consistent echo
implementation, you can override it with your own function.
无论外壳如何,它的行为都一样.
This will behave the same, regardless of the shell.
echo(){ printf %s\\n "$*";}
echo "a\nb"
这篇关于zsh和bash之间的回声有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!