问题描述
我用相同的命令 echo -ne"hello \ n"
使用bash和破折号来获得不同的行为.见下文:
I got different behaviors with the same command echo -ne "hello\n"
with bash and with dash. See below :
$ bash -c 'echo -ne "hello\n"'
hello
$ dash -c 'echo -ne "hello\n"'
-ne hello
那是为什么?我根本听不懂...
Why is that ? I don't understand at all…
我的系统:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 12.04.5 LTS
Release: 12.04
Codename: precise
推荐答案
echo
的POSIX规范不支持任何参数.在此处查看.
The POSIX specification for echo
doesn't support any arguments. See it here.
尽管规范中提到了 -n
,但这是说它不是不是选项,或者是实现定义的情况,或者被视为字符串.
And while the spec mentions -n
it does so to say that it is not an option and is either an implementation defined case or to be treated as a string.
所以 dash
正是这样做的.
bash
在许多方面具有不合格的行为.
bash
, on the other hand, has non-conforming behavior in a number of ways.
这就是为什么通常不建议使用 echo
来使用 printf
的原因,后者具有更好的规范和更好的可移植性.
This is why the use of echo
is generally discouraged in favor of the using printf
which has a much better specification and much better portable behavior.
这篇关于使用命令`echo -ne'hello \ n'`进行Bash vs. Dash行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!