使用命令`echo -ne'hello \ n'`进行Bash vs. Dash行为 [英] Bash vs. Dash behavior with the command `echo -ne "hello\n"`

查看:60
本文介绍了使用命令`echo -ne'hello \ n'`进行Bash vs. Dash行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用相同的命令 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行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆