echo difference in sh and bash

  • Permalink
  • submit to reddit
  • Email
  • 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

See related articles to this posting


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
comp.unix.shell 15057 articles. 25 followers. Post

7 Replies
464 Views

Similar Articles

[PageSpeed] 11


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

difference between sh and bash
Hi, When executing a shell script on solaris (/bin/sh) the following line works fine: eval `$mozilla_home/bin/mozilla -remote \'ping()\' < /dev/null` but when run on linux (/bin/bash) I get the following error: line 482: unexpected EOF while looking for matching ``' Any one know I can make the following line execute in both sh and bash? cheers, //mikael On Fri, 07 Oct 2005 10:55:26 +0200 Petterson Mikael <mikael.petterson@era.ericsson.se> wrote: > When executing a shell script on solaris (/bin/sh) the following line > works fine: > > eval `$mozill...

[bash|sh] prompt display differences
Hi all (again), In bash I had set my prompt to: export PS1='\u@\h ) \W )' I have recently converted (by force) to sh-posix, which does not seem to recognize the above shortcuts. I have easily been able to convert the "\u" to $USER and "\h" to $HOST, but I am stuck on the "\W". Just in case, "\W" is supposed to be dynamically updated to `basename $PWD`. The only thing that I have now is: export USER=`whoami` export HOST=`hostname` BPWD=`basename $PWD`; export PS1='$USER@$HOST > $BPWD >' alias cd='cd $@; BPWD=`basename $PWD`; ...

[SH BASH] Echoing no more than X chars per line
Hello everybody, I have a small question: In most terminals a line can hold up to 80 (or so) chars. But many times, in a sh script, there is a need to echo out a string with more than this amount, and if shell variables are included in the string, it is hard to know it's max length. There result of echoing a long string to the terminal is that it breaks up ugly, overflows to the next line, with words broken up, etc. My question is this: Is there any printing command (such as echo) that can also receive an integer telling the maximum chars that should be printed per line, and echoes t...

Bash Shell wildcard handling [root@pudasin31 test]# echo [A-B]* A b B [root@pudasin31 test]# echo [A-D]* BASH Shell wildcard handling wrong. [root@pudasin31 test]# csh [root@pudasin31 test]
Hi Group It seems bash shell is handling the "range" wildcard construct differently . The actual directory contents : ====================================== [root@jnair test]# ls -a .. .. a A b B c C d D [root@jnair test]# ====================================== Below I try to expand a UpperCase range but the output also shows LowerCase entries. [root@jnair test]# bash [root@jnair test]# echo [A-D]* A b B c C d D ======================================== While the "csh" C Shell is handling it correctly. [root@jnair test]# csh [root@jnair test]# echo [A-D]* A...

Difference between /sbin/sh and /usr/bin/sh
Under HPUX 10.20 and higher, is there a difference between /sbin/sh and /usr/bin/sh or are they the same? -- Mike Shapp mikeshapp@yahoo.com Mike Shapp <mikeshapp@yahoo.com> wrote: > Under HPUX 10.20 and higher, is there a difference between /sbin/sh and > /usr/bin/sh or are they the same? /sbin/sh is statically linked (i.e. no shared libraries) so that it can run without /usr mounted, and has no local language support. root *MUST* have /sbin/sh as hir shell. /usr/bin/sh is dynamically linked (i.e. uses shared libraries), and has local language support. /usr/bin/sh is the/...

why is echo different?
IFS=: echo $IFS # the result is space echo "$IFS" # the result is : Is there any difference between $IFS and "$IFS" Parmenides wrote: > IFS=: > echo $IFS # the result is space a space, or just a blank line? > echo "$IFS" # the result is : > Is there any difference between $IFS and "$IFS" Also $ aaa="a > b > c > d > e" $ echo $aaa a b c d e $ echo "$aaa" a b c d e IFS is the internal field separator. When you quote a string with double quotes it is a sin...

Echo in Echo
Hi there, First of thank you for reading. I have the following issue. I'm filling a select list dynamically with mysql and want the names that are retrieved from de database to be changed by a DEFINE that stated in other php file.. The first part works the names al get in the list but the DEFINE isn't happening which is logic cause you have only one echo which is the name from the database. But putting an echo around that one doesn't work, does any of you have a idea how to solve my issue. Code: <select id="objectTypeSelect" tabindex="2"> <?php...

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="',...

env -i x=9 bash -c 'x=4;bash -c "echo x: /\$x/"' # then try w/o 'x=9'
Bug or feature? Pls explain: /tmp $ env -i bash -c 'x=4;bash -c "echo x: /\$x/"' x: // /tmp $ env -i x=9 bash -c 'x=4;bash -c "echo x: /\$x/"' x: /4/ --snip ~ $ uname -r; echo $BASH_VERSION 2.6.27.41-170.2.117.fc10.i686 3.2.39(1)-release -- thx On 2010-05-15, Name withheld by request <anonb6e9@nyx3.nyx.net> wrote: > Bug or feature? Pls explain: > > /tmp $ env -i bash -c 'x=4;bash -c "echo x: /\$x/"' > x: // > /tmp $ env -i x=9 bash -c 'x=4;bash -c "echo x: /\...

bash echo issue
Why these two code snipets produce different results ? SALE=`echo -e "linea 1"` SALE=`echo -e "$SALE\nlinea 2"` echo -e "$SALE" Output: linea 1 linea 2 SALE=`echo -e "linea 1\n"` SALE=`echo -e "$SALE linea 2"` echo -e "$SALE" Output: linea 1 linea 2 What I'm missing ? hans http://www.paraguaycoins.com/ In article <alpine.LNX.2.00.1109270830520.2897@betelgeuse>, Hans Goossen <goossen@gmx.com> wrote: >Why these two code snipets produce different results ? > >SALE=`echo -e "linea 1"` ...

Difference between BASH and Kornshell
Can someone explain this to me: Here is my test program: echo "this is a test" | while read one two three four do echo "Inside the loop" echo "One is $one" echo "Two is $two" echo "Three is $three" echo "Four is $four" done echo "Outside the loop" echo "One is $one" echo "Two is $two" echo "Three is $three" echo "Four is $four" When I run this in Kornshell, I get this: Inside the loop One is this Two is is Three is a Four is test Outside the loop One is this Two...

bash echo problem...
Greetings: I have a file like so: Altera 1805@system1 (Quartus) Ansoft 1670@system1 (Signal Integerty Tool) Cadence 5280@system2 (PCB Board Layout Tools) # Denali 1783@system2 (Memory Models for Simulation) .... I have the following bash code: for line in `cat license.lst | grep -v ^# | awk '$2 ~ /^[0-9]+/ {print $0}'` do echo "${line}" done When this runs I get: Altera 1805@system1 (Quartus) Ansoft 1670@system1 (Signal Integerty Tool) Cadence 5280@system2 (PCB .... I have run dos2unix on...

indirection in sh (bash)
i would like to substitute some data into a template file, and what i was inspired to do is attached below: 001| #!/bin/sh 002| 003| a="s/SS/2/ s/TT/5/ s/WW.W/-45.0/ s/YY.Y/+35.6/ s/ZZ/+104.2/ s/QQ/36.0/" 004| b="s/SS/3/ s/TT/4/ s/WW.W/-45.0/ s/YY.Y/+35.6/ s/ZZ/+104.2/ s/QQ/36.0/" 005| c="s/SS/4/ s/TT/3/ s/WW.W/-45.0/ s/YY.Y/+35.6/ s/ZZ/+104.2/ s/QQ/36.0/" 006| d="s/SS/5/ s/TT/2/ s/WW.W/-45.0/ s/YY.Y/+35.6/ s/ZZ/+104.2/ s/QQ/36.0/" 007| 008| for i in a b c d; do 009| cmd=... 010| sed $cmd < file1 | command > file1.$i 011| done let's m...

Bash echo issues
All, I am trying to port scripts to linux platform. The way the shell script works on other platforms is echo "checking for something ... \c" Check something if everything is alright then echo "OK" OUTPUT would be "checking for something ... OK" Essentially ot comes out as one line. This main script also calls several other scripts which inturn does the same kind of checking. On Linux, the output would be checking for something ... \c OK I would like to have uniformity. I have sieved through the newsgroups but I did not see any authoritative answer. I have tri...

bash echo question
Hello - How can a string echoed from a background task, launched from terminal #1, display persistently at a given scrn location during foreground task display on term #1? What about displaying persistently on all active terminals? thanx in advance, mark jonathan horn 2006-08-12, 06:58(-07), mark_horn@sbcglobal.net: > Hello - > > How can a string echoed from a background task, launched from terminal > #1, display persistently at a given scrn location during foreground > task display on term #1? What about displaying persistently on all > active terminals? [...] You may...

Difference between echo and php
Dear Friends , I am new to php language , what is difference between print statement and echo...? Thanks In article <d8fd1e99-3f1d-4841-a931-b5678d273b3b@d9g2000prh.googlegroups.com>, pavunkumar <pavun.bks@gmail.com> wrote: > I am new to php language , what is difference >between print statement and >echo...? http://tinyurl.com/pqwzbd On Thu, 13 Aug 2009 04:40:36 -0700 (PDT), pavunkumar <pavun.bks@gmail.com> wrote: >Dear Friends , > > I am new to php language , what is difference >betwe...

What is the difference between echo and print
Hi guys Can someone please explain what the difference is between echo and print in unix (Bash). I'm using cygwin and don't have a unix environment to test in. I'm curious to see the following: print -r $PATH versus echo $PATH Needless to say the latter shows the path but for some reason, when using cygwin I get an error for the print -r $PATH. So would someone please run the two in unix and show me the output? On Wed, 01 Jun 2005 at 10:28 GMT, ceauke@yahoo.com wrote: > Hi guys > > Can someone please explain what the difference is between echo and > print in unix...

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? ...

Difference between BASH and Kornshell #2
Can someone explain this to me: Here is my test program: echo "this is a test" | while read one two three four do echo "Inside the loop" echo "One is $one" echo "Two is $two" echo "Three is $three" echo "Four is $four" done echo "Outside the loop" echo "One is $one" echo "Two is $two" echo "Three is $three" echo "Four is $four" When I run this in Kornshell, I get this: Inside the loop One is this Two is is Three is a Four is test Outside the loop One is this Two...

Different developers dislike osx for different reasons: many different rootcauses for many different symptoms
As you might have remembered I have expressed my disdain for xcode on numberous occasions for the system being completely unusable when debugging in tethered mode. Some of you suggested that core duo mac mini with 2GB or ram is not suitable for development. Okay, so our admin has provided me with a 4GB ram 2.7Ghz pc (1.33Ghz bus), initially core duo and now a 2.7Ghz core quad: still I have xcode text editor crawling when gdb is running. Same deal as on core duo: system idle is around 50-75% and yet xcode (3.2.2) is slow: i'll blame this on synchronization done incorrectly or th...

BASH: Can bash do this?
I input a long command in bash, and then I found I must run some other command such as man, but I must not run this long command to save it in the history, because it may harmful, Is there a way to save it in the history without running it? On 17 Dec 2006 19:10:48 -0800, "hantechs@gmail.com" <hantechs@gmail.com> wrote: >I input a long command in bash, and then I found I must run some other >command such as man, but I must not run this long command to save it in >the history, because it may harmful, Is there a way to save it in the >history without running it? #c...

bash question ... echo subtleties
Consider (echo "$X" >$file) [this is bash]. What if $X is "-n" ? That would break the code. What is good way to make (echo "$X" >>$file) work in all cases ? Y.L. "Yakov" <iler.ml@gmail.com> schrieb im Newsbeitrag news:97f76b73-4355-48c3-bb0b-7054001d4c8c@w28g2000hsf.googlegroups.com... > Consider (echo "$X" >$file) [this is bash]. > > What if $X is "-n" ? That would break the code. > > What is good way to make (echo "$X" >>$file) work in all cases ? Use (printf "%s\n" &qu...

how to use ./example.sh instead of sh ./example.sh
Hi, I feel a little silly asking this question, I searched on Google and Google Groups but could not find an answer... I'm hoping someone here can help me out. When I had Debian installed on a little test PC I used to execute scripts using "./example.sh" Recently I upgraded my test server and installed Red Hat 8.0, but now to execute scripts I need to use "sh ./example.sh" As a result some of the applications I was using no longer work. Can anyone explain how to execute scripts without the sh, or point me to a relevant resource on the Internet. Regards, Paul Hi! ...

bash (sh?) scripting question
Hi, I am trying to implement a type of "try" function, that will execute a command and verify the exit code: try() { eval $* if [ $? -ne 0 ]; then echo "failed" exit $? fi } I wish to send it a command like this: SOMEFILE="/some path/with spaces" mv "$SOMEFILE" "$SOMEFILE.backup" But the quotes are causing a problem: try "mv \"$SOMEFILE\" \"$SOMEFILE.bak\"" try mv "$SOMEFILE" "$SOMEFILE.backup" Both of these last 2 lines fail. I used this to see how parameters...