echo difference in sh and bash

  • Follow


The echo command in Bourne shell and bash are not the
same, e.g. /c in sh isn't treated the same in bash as
in sh. What is the proper way to use echo so that the
script works the same way in sh and bash?

/Why Tea 
0
Reply ytlim1 (236) 2/8/2011 11:37:47 AM

Why Tea wrote:

> The echo command in Bourne shell and bash are not the
> same, e.g. /c in sh isn't treated the same in bash as
> in sh. What is the proper way to use echo so that the
> script works the same way in sh and bash?

Use printf.

0
Reply pk 2/8/2011 11:38:39 AM


Why Tea wrote:

> The echo command in Bourne shell and bash are not the
> same, e.g. /c in sh isn't treated the same in bash as
> in sh. What is the proper way to use echo so that the
> script works the same way in sh and bash?
> 
> /Why Tea

.... or use /bin/echo (or maybe /usr/bin/echo, depending on the system) 
instead.

Helge

0
Reply Helge 2/8/2011 11:51:56 AM

In article <iir9vu$1ja$2@speranza.aioe.org>, pk  <pk@pk.invalid> wrote:
>Why Tea wrote:
>
>> The echo command in Bourne shell and bash are not the
>> same, e.g. /c in sh isn't treated the same in bash as
>> in sh. What is the proper way to use echo so that the
>> script works the same way in sh and bash?

You're premise is wrong.  /c is treated the same in all shells.

>Use printf.

Good advice.

-- 
"We should always be disposed to believe that which appears to us to be
white is really black, if the hierarchy  of the church so decides." 

    - Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order -

0
Reply gazelle 2/8/2011 1:39:22 PM

2011-02-08, 12:51(+01), Helge Blischke:
> Why Tea wrote:
>
>> The echo command in Bourne shell and bash are not the
>> same, e.g. /c in sh isn't treated the same in bash as
>> in sh. What is the proper way to use echo so that the
>> script works the same way in sh and bash?
>> 
>> /Why Tea
>
> ... or use /bin/echo (or maybe /usr/bin/echo, depending on the system) 
> instead.
[...]

Wrong advice. That won't make your script more portable.

The things you can expect from a random implementation of echo
is that it will output its arguments separated by spaces and
followed by a newline character as long as its first argument
doesn't start with "-" and none of its arguments contain
backslashes.

POSIX gives slightly more guarantees (though not very useful),
but some implementation (including bash's) don't respect them.

So, yes, use "printf" instead, that's what POSIX recommands as
well.

-- 
Stephane
0
Reply Stephane 2/9/2011 11:21:38 AM

Stephane CHAZELAS <stephane_chazelas@yahoo.fr> writes:

> > Why Tea wrote:
> >> What is the proper way to use echo so that the script works the
> >> same way in sh and bash?
[…]

> Wrong advice. [Using an explicit path to the ‘echo’ program] won't
> make your script more portable.

Until we know what the OP means by “proper way”, we don't know whether
“not optimally portable” is compatible with their meaning :-)

> So, yes, use "printf" instead, that's what POSIX recommands as well.

Still good advice.

-- 
 \          “That's all very good in practice, but how does it work in |
  `\                                             *theory*?” —anonymous |
_o__)                                                                  |
Ben Finney
0
Reply Ben 2/9/2011 12:41:43 PM

Stephane CHAZELAS wrote:
> 2011-02-08, 12:51(+01), Helge Blischke:
>> Why Tea wrote:
>>
>>> [...]What is the proper way to use echo so that the
>>> script works the same way in sh and bash?
>>> 
>> ... or use /bin/echo (or maybe /usr/bin/echo, depending on the system) 
>> instead.
> [...]
> 
> Wrong advice. That won't make your script more portable.
> 
> The things you can expect from a random implementation of echo
> is that it will output its arguments separated by spaces and
> followed by a newline character as long as its first argument
> doesn't start with "-" and none of its arguments contain
> backslashes.
> 
> POSIX gives slightly more guarantees (though not very useful),
> but some implementation (including bash's) don't respect them.

and http://www.in-ulm.de/~mascheck/various/echo+printf/ tries
to illustrate the history (why different implementations?)
and the actual variations.
0
Reply Sven 2/9/2011 9:37:01 PM

On 2011-02-08, Why Tea <ytlim1@gmail.com> wrote:
> The echo command in Bourne shell and bash are not the
> same, e.g. /c in sh isn't treated the same in bash as
> in sh.

Yes it is.

> What is the proper way to use echo so that the
> script works the same way in sh and bash?

Don't.  Use printf(1).

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
0
Reply Seebs 2/10/2011 8:21:36 PM

7 Replies
241 Views

(page loaded in 0.122 seconds)

Similiar Articles:













7/27/2012 4:56:13 PM


Reply: