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 15067 articles. 25 followers. Post

7 Replies
438 Views

Similar Articles

[PageSpeed] 58


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

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

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

sh / bash as serial terminal
Hi, There are countless recipes out there on how to connect to a unix host using a serial terminal, or an emulator running on a (different) computer. This is not what I want to do. Is there not a way I can make a shell (bash, whatever) talk with a serial port on the host on which it is running, instead of having to use a "terminal emulator" program? I often need to connect serial hardware to different linux boxes, and I don't want to install software on each of them if there is some way I can have OS components do the same... Kind regards Soren Kuula On 2011-11-13, s_kuula &l...

difference between echo and gettext commands
Hi All, Running on Solaris 9. I've got a question regarding the difference between the echo and the gettext commands. I've seen in some sh scripts the following command : echo `gettext "Attempting to ...."` What is the difference with the command : echo "Attempting to ..." ??? Thanks. BRegards, Christian. christian wrote: > I've seen in some sh scripts the following command : > > echo `gettext "Attempting to ...."` > > What is the difference with the command : echo "Attempting to ..." ??? > See the gettext(1) manp...

password no echo in bash script
Hi, Is it possible to create a script that requires a password that isn't revealed when the user types it in. Identical to when you login with your username and passwd. I've looked all over the place but couldn't find anything on the subject. -- Thanks, Don On Mon, 20 Oct 2003 at 19:53 GMT, Don Low wrote: > Hi, > > Is it possible to create a script that requires a password that isn't > revealed when the user types it in. Identical to when you login with > your username and passwd. I've looked all over the place but couldn't > find anything on the...

sh/bash --> tcsh
I'm not very familiar with tcsh but was asked to do a sript for someone and I've run into a snag. In sh/bash I can do the following: if ( echo $cplargs | grep -- -g >/dev/null ); then olvl='-O0' fi Is there an equivalent in tcsh? I've tried adding (`): if ( `echo $cplargs | grep -- -g >/dev/null` ) then olvl='-O0' endif I either get a syntax error or I get the same results if $cplargs has a -g or not. -- e-Mail: chris at cwaiken dot net Home: www.cwaiken.net In article <slrnc4buc1.f1o.cwaiken@panix3.panix.com>, Christopher W Aiken &...

[bash] Command line echo
I have some script. For instance, ------ Script foo ------ date uname ------------------------ I would like the command line to be shown before its executing, i.e. $ foo > date Sun Jun 20 14:07:46 2004 > uname CYGWIN_NT-5.0 -- Alex Vinokur http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn l "Alex Vinokur" <alexvn@big.foot.com> wrote in message news:2jl9qmF12e6fqU1@uni-berlin.de... > I have some script. > For instance, > > ------ Script foo ------ #! /usr/bin/bash -x > date > uname > -------...

behaviour of echo in a bash script.
Hi, I have a program echo2.sh which executes 2 lines. 1 - echo () and 2 - echo "()" however when it tries to execute line 2 it gives an error. "syntax error near unexpected token `echo'" so what exactly is happening when i do ../echo2.sh it first executes echo whose only argument is () which creates a subshell. now when it tries to execute the 2nd echo why is it giving an error message ? Any help, information is appreciated. Thanks. Yashesh Bhatia. Progam Details ------------------------------------------------------------ [yvb@bfc1 ch3]$ bash --version G...

Difference between BASH and Kornshell #3
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...

Difference between BASH and Kornshell #4
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...