echo difference in sh and bash

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
ytlim1 (236)
2/8/2011 11:37:47 AM
comp.unix.shell 15263 articles. 0 followers. Post Follow

7 Replies
545 Views

Similar Articles

[PageSpeed] 50
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
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
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
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
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
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
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
Seebs
2/10/2011 8:21:36 PM
Reply:
Similar Artilces:

Taking data from 2 boards at different speeds
I am working on a project that requires the monitoring of temperature and pressure in an internal combustion engine. For some reason, the lab is running labVIEW 6, even though we have a free site license for 8.5. We are using a PCI 6070E and a PCI 4351 connected to TC-2190 and BNC-2010 terminal blocks. I need to collect data from these two at the same time, when I do this the data from the BNC overruns its buffer, and the TC has erratic read rates. They need&nbsp;to read at different rates.&nbsp;I am pretty new to labVIEW and my code is a mess, so I won't post it as it is nowhere n...

Implementing NLMS for Echo cancelation in Speech
Hi I am trying to write an acoustic/line echo canceller. I choose the quite well known nlms algo. First I tried to implent it in matlab. When I checked the algo with any "test input" like sin wave, zigsaw etc, I did get convergance. Then I tried it with speech, but failed. I tried to improve it by implementing anti-correlation filter Pre-whitening). I did saw minimal improvments in the filter taps, and i terms of math output, there is error reduction ( not much). Still, whe you play the speech, you dont hear a difference. I also implemented filter which cut all 0-100Hz fully, 100-...

Echo GP
Does anyone have a manual (preferably scanned) for the Echo GP? I can find lots of info for the Echo cards, but nothing for the standalone GP. I know the one I have works, as I plugged it into my PC's serial port and it speaks. There are supposed to be special commands that you can send to the device to control pitch, speed, pronunciation of punctuation, phoneme mode, etc., but I don't have any of that info. I have Googled extensively without success. ...

Synchronous but different iterators
(somewhat contrived example:) Say I have 2 different data structures which I want to iterate over, and Tree, and an Array. I had to write specific iterators for my Tree structure, such as depth_first, breadth_first, etc. Is there a good way to iterate over each structure in a synchronous manner, but with a different iteration style? For example, I want to step through my Array normally using each_index, but want to step through my Tree structure using, say, depth_first, but I want to do this concurrently because of what I want to do to/with the data. "pseudo" ruby code: (myTree.d...

Difference between net and reg #2
Hi, Can anybody tell me what is difference between net and reg??when they are supposed to use? thanks ...

Scary IPC::Open3 filehandle difference
Hi, I was experimenting with Open3 and wrote the following two programs: ======buggy.pl: #!/usr/local/bin/perl -w use strict; foreach my $i (1 .. 100) { print STDERR "STDERR $i\n"; print "STDOUT $i\n"; } ======buggy.pl: #!/usr/local/bin/perl -w use strict; use IPC::Open3; use IO::Select; print "starting wrapper...\n"; my $pid = open3(\*WTRFH, \*RDRFH, \*ERRFH, @ARGV); my $s = IO::Select->new(); #$s->add($wtrfh); $s->add(\*RDRFH); $s->add(\*ERRFH); my $timeout = 1; my $first_timeout = 3600; my @ready = $s->can_read($first_t...

echo commands
I have a shell script which contains some shell commands to be executed. The shell script when executed doesn't echo the commands it is executing. How to make it echo the commands it is executing. Ofcourse, one obvious way is to put every command in an echo statement. The problem with this if I modify the command, I will have to modify it in two places. Is there any other way to achieve this? TIA, Ajar Ajar wrote: > I have a shell script which contains some shell commands to be > executed. > The shell script when executed doesn't echo the commands it is > executing. How...

How can I combine two different time data and calculate the difference
If data like it: I want to calculatethe the left_bq, left_sq, left_bp, and left_sp if buy_p=>sell_p. What can I do in sas? datetime bp bq sp sq left_bp left_bq left_sp left_sq 02Jan07:08:30 6 4 02Jan07:08:32 3 2 6 2 3 0 02Jan07:08:33 5 4 6 0 5 2 02Jan07:08:34 7 6 7 4 5 0 ...

echo canceller
How an echo canceller distinguishes that an incoming stream of bits are of voice or data. So that it should not cancel data bits considering voice echo. <pankajjd@rediffmail.com> wrote in message news:1e5efa11.0401240729.6b2a568c@posting.google.com... > How an echo canceller distinguishes that an incoming stream of bits > are of voice or data. So that it should not cancel data bits > considering voice echo. It's all data isn't it? The issue is reliably reconstructing the bits at the output. If there's echo / multipath, then that's still the objective. Or am ...

sort result different on 2 servers.
Hi, all: The sort result is different on 2 servers. One of them seems to be wrong. There are 2 Linux servers in our office. Both are the same model. Processor: AMD Dual core 2218 Machine: Tyan GT24/3392 One server (Let's call it server A now) has to be accessed from a Solaris machine by using qrsh -q <server_name>. The Solaris machine is accessed through a Windows software Cygwin. The other server (Let's call it server B now.) can be opened directly using Cygwin from my Windows system. The sort command is identical on the 2 Linux servers. Both look like below: # which sor...

Date Different
Hi How do I calculate number of days different? ex: beg date 20060205 end date 20060303 I need to know how many days different? What is the function syntax to handle this? thanks for your help Tom You can use the %diff built in function to return the difference between two date, time or timestamp values. For example: day_diff = %diff(end_dat::beg_date:*days); Hope this helps "tom" <tomwu1@optonline.net> wrote in message news:GX9Of.591$EK1.79@fe10.lga... > Hi > How do I calculate number of days different? > ex: beg date 20060205 > end date 20060303 > I ne...

what's the difference between sequemtial memory access and non-sequemtial memory access
From the viewpoint of Address bus, it always has the same update approach. For example, case 1: Cycle..............N-Cycle....S-Cycle....S-Cycle... Address Bus...........a.........a+4........a+8..... case 2: (if this case is possible) Cycle..............N-Cycle....N-Cycle....N-Cycle... Address Bus...........a.........a+4........a+8..... ARM always update the address bus no matter what the cycle is. Then why there is N-Cycle and S-Cycle. There is some other control signal has different behavior between these two cycles? What's the benefit of these two cycles? Jackshowme ...

echo or not to echo?
I have a couple pages that have tables, whats best use echo to produce the full table with the variables or is it best to just make the table in plain html and use <?php echo ('$va'l); ?> when i need to see some value. Example i have tables like this echo '<div align="',$align,'"><table border="0" cellpadding="2" cellspacing="0" width="',$largeur,'">'; echo '<tr><td bgcolor="',$titre_bgcolor,'"><font face="',$titre_face,'" size="',...

[OT] Re: Can I Create View with UNION of two different libraries ?
Jean-Philippe BALLAT racontait dans comp.sys.ibm.as400.misc: Bonjour > cela me gave. L�, j'ai peur que nos amis anglophones aient du mal � comprendre :-) -- Cordialement/Best regards Marc Rauzier Marc Rauzier wrote: > Jean-Philippe BALLAT racontait dans comp.sys.ibm.as400.misc: > > Bonjour > > >>cela me gave. > > > L�, j'ai peur que nos amis anglophones aient du mal � comprendre :-) Je ne suis pas parfaitement bilingue, mais je l'ai compris. Tr�s marrant! 1. Mots enlev� du contexte. Merci de bien vouloir "Mettre la phrase c...

webinterface to bash
Hi! To relief the problems of accessing a unix machine from behind a restrictive firewall or from an internet cafe I started to make a PHP web interface to bash. I'd like to hear your opinions and advice about my concept, especially regarding security. There is already such a thing (http://www.rohitab.com/cgiscripts/cgitelnet.html). However, it lacks interactive input to programs. To fix this I'd use a frame that displays the output of login, which turns into a shell that will run our commands. The whole session output will be a single http page. The input will be through a f...

Reply address different to the From address
I am using Eudora Pro 6.2 I have recently changed the dominant account by editing in Tools > Options > Getting Started Each new message carries the new dominant address in the "From" field. However when I "Reply" to a message the "From" field is the previous dominant address. Where is that pesky critter hiding? Thanks, Robin Chapple Robin Chapple <robinski@anti_spammelbpc.org.au> wrote: > I am using Eudora Pro 6.2 FYI, there is no such thing as "Eudora Pro 6.2". The last version of Eudora that had "pro"...

what's the difference between the different the different GraphicConfigurations
I'm looking through this example http://java.sun.com/j2se/1.3/docs/guide/2d/spec/j2d-awt.fm6.html, and I cannot understand the difference between the different GraphicConfigurations. When the frames come up on my screen, I get eight of them on the screen, and the only difference I can tell between them is the pxlfmt in the GraphicsConfiguration.toString(). What is the difference between them? I'm trying to figure out which GraphicsConfiguration I need to draw to if I want to do some drawing on multi-screened application? Do I have to draw to all of the GraphicsConfiguration's? ...

calculating difference between two times
Anyone know how to calculate the difference between two times displayed in two textboxes? I'm starting out with two textboxes: "txtCallTimeBegins" & "txtCallTimeEnds" this yielded: 6/7/2007 2:40:50 PM & 6/7/2007 2:45:04 PM respectively. First I have coverted these to two additional textboxes to hold only the time, not the date using: =TimeValue([txtCallBeginTime]) & =TimeValue([txtCallEndTime]) So I end up with: 2:40:50 PM 2:45:04 PM txtConvertedBegin & txtConvertedEnd textboxes So I then want to get the difference of the two: I tried =([txtConv...

Different WIA behavior of MS Office 2003 SP2
Hello!, I'm developing a wia driver for a scanner, But I can't understand the different behavior of MS Office 2003 SP1 and MS Office 2003 SP2. I have a mirrored and rotated image in MS Office SP2. Does SP2 change stacking method of the transfered data? Do you know why and How to overcome? Thank you ...

plot with different ranges
I want a fit function to be plotted only in a special range: plot [x=0:400] 'Data.txt' using 1:2, [x=100:400] f(x) however seems not to work. Is this somehow possible ? If not, then this could mean that I would have to use another tool, simply because I can not print a fitfuntion in the whole graph because that would mean that the fit is relevant in the whole plot which is not true. (gnuplot 4.1) Matthias -- www.matthiaspospiech.de Matthias Pospiech <matthiasPUNKTpospiech@gmx.de> writes: > I want a fit function to be plotted only in a special range: ...

how to close echo in REPL
Hi there, I am new to common lisp, and just do some exercise in REPL. REPL will echo for every input. However, when I hope to make a vector with length 1000000, the echo process will take a long time. My questions, how to close the echo in the following situation: (defun random-vector (n limit) (let ((result (make-array n))) (dotimes (i n result) (setf (svref result i) (random limit))))) (setf long-vector (random-vector 1000000 1.0d0)) By the way, I defined a function named dot: (defun dot (a b) (reduce #'+ (map 'vector #'* a b))) ...

Different iostat results
A single line compiled with pgf90 gives very different results on Linux and Unix. Here it is: > k=0; read(unit=text(1:start(k+1)-1),fmt=*,iostat=stat(k+1)) n The referred text variable is "FEMOUTER, 1,6". It may be filled with some breaklines. On Unix, stat(1) becomes 1083, while on Linux it is 0. Does anyone know the reasons behind this? Regards, H�kan Lane Chalmers (Remove CUT., SPAM. and PLEASE. for email address) H�kan Lane <Hakan.CUT.Lane@sm.SPAM.chalmers.PLEASE.se> writes: > A single line compiled with pgf90 gives very different results on Linux > and U...

Re: How to declare different length for different dimension of #4
Yiyu, What can you do with a _TEMPORARY_ array that cannot not be done with length _temporary_1 - _temporary_100 $ 5 _temporary_101 - _temporary_300 $ 1 ; array _temporary_ (100,3) _temporary_1 - _temporary_300 ; retain _temporary_ ; drop _temporary_: ; Or perhaps a better question is, what will you do with the 800 bytes that be saved by not using array _temporary_ (100,3) $ 5 _temporary_ ; In general, non-problems are better not solved. Ian Whitlock ================== Date: Thu, 19 May 2005 09:48:24 -0700 Reply-To: Yiyu <shenyiyu@GMAIL.C...

FAQ 7.7 Why do Perl operators have different precedence than C operators? #4 228521
This is an excerpt from the latest version perlfaq7.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 7.7: Why do Perl operators have different precedence than C operators? Actually, they don't. All C operators that Perl copies have the same precedence in Perl as they do in C. The problem is with operators that ...

differences between version
--e0cb4e887cabcd6273048db34012 Content-Type: text/plain; charset=ISO-8859-1 hi , i want to know what's the differences between bind 9 version (especially between 9.4 and 9.5/6/7) , where can i find a table that can describe it? i tried to google it but i didn't anything useful , . can someone assist me here? thanks in advance , Ram --e0cb4e887cabcd6273048db34012 Content-Type: text/html; charset=ISO-8859-1 <div dir="ltr">hi , <br>i want to know what&#39;s the differences between bind 9 version (especially between 9.4 and 9.5/6/7) , <...