回声产出的bash脚本-e参数。我怎样才能prevent呢? [英] echo outputs -e parameter in bash scripts. How can I prevent this?

查看:119
本文介绍了回声产出的bash脚本-e参数。我怎样才能prevent呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过回声手册页,它告诉我,-e参数将允许转义字符,如换行符转义N,有其特殊的意义。当我键入​​命令

I've read the man pages on echo, and it tells me that the -e parameter will allow an escaped character, such as an escaped n for newline, to have its special meaning. When I type the command

$ echo -e 'foo\nbar'

到交互式bash shell中,我得到预期的输出结果:

into an interactive bash shell, I get the expected output:

foo
bar

但是当我使用这个相同的命令(我试过的字符作为测试情况下,这个命令字符)我得到以下的输出:

But when I use this same command (i've tried this command character for character as a test case) I get the following output:

-e foo
 bar

这是因为如果回声间pretting的-e作为参数(因为新行仍显示),但它也跨$ P $点的-e作为一个字符串呼应。这里发生了什么?我怎样才能prevent显示了-e?

It's as if echo is interpretting the -e as a parameter (because the newline still shows up) yet also it interprets the -e as a string to echo. What's going on here? How can I prevent the -e showing up?

推荐答案

您需要使用#!/斌/庆典在你的脚本的第一行。如果你不这样做,或者如果您使用#!/ bin / sh的,该脚本将由Bourne shell和它的回声不承认 -e 选项。一般情况下,建议所有新脚本中使用的printf 而不是回声如果便携性是非常重要的。

You need to use #!/bin/bash as the first line in your script. If you don't, or if you use #!/bin/sh, the script will be run by the Bourne shell and its echo doesn't recognize the -e option. In general, it is recommended that all new scripts use printf instead of echo if portability is important.

在Ubuntu中, SH 是一个符号链接提供的 /斌/破折号

In Ubuntu, sh is provided by a symlink to /bin/dash.

这篇关于回声产出的bash脚本-e参数。我怎样才能prevent呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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