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 15359 articles. 0 followers. Post Follow

7 Replies
602 Views

Similar Articles

[PageSpeed] 46

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:

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

web hosting, shell provider, unix shells, bnc shell, ircd provider, shell hosting, bnc shells, irc shells, ircd shells, unix shell account, irc shell, irc, eggdrop shell, irc vps, irc shell, linux vps
http://www.hostrazor.net/ web hosting, shell provider, unix shells, bnc shell, ircd provider, shell hosting, bnc shells, irc shells, ircd shells, unix shell account, irc shell, irc, eggdrop shell, irc vps, irc shell, linux vps -- questo articolo e` stato inviato via web dal servizio gratuito http://www.newsland.it/news segnala gli abusi ad abuse@newsland.it ...

esc K does not work in Linux sh shell or bash shell
In Solaris I used esc k to pull back previous commands and a / to search In Linux I can use the arrows to go back but I would like to be able to easily search for a particular command. I use the bash and sh shells for interactive access. JAW <jwilliam@aglresources.com> wrote: > In Solaris I used esc k to pull back previous commands and a / to > search > > In Linux I can use the arrows to go back but I would like to be able > to easily search for a particular command. > > I use the bash and sh shells for interactive access. You can either configure bash to use...

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

Differences between different shells
Will there be any differences in the execution of the following command for the various unix shells: mv "file name1" "file destination" 2>&1 In particular, the effects of the redirection and the double quotes used for the filenames. Thanks. Eli ps. please cc reply to eli@napchan.com On Wed, 01 Oct 2003 at 12:52 GMT, eli napchan wrote: > Will there be any differences in the execution of the following > command for the various unix shells: > mv "file name1" "file destination" 2>&1 > > In particular, the effects of the redi...

[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`; ...

Running same unix script in different shells
How can I port same shell script in to different shells like C Shell, K Shell and Bourne Shell Chitta c_jena@ureach.com writes: > How can I port same shell script in to different shells like C Shell, K > Shell and Bourne Shell Launching your favorite editor, reading the man page of the source and the target shell, thinking, and editing the script file. Also, it's possible to write a single source file usable both in csh, sh, Fortran and C, and perhaps it would be possible to add more languages, so you would not need to "port" the script... happy.source: ----------...

Bash shell to Python shell?
I am sure you have not read a question this basic in some time, but I am curious. Using Linux I open a terminal window and type: python. Does Bash Shell go away and to become a Python Shell, or is it still a Bash Shell with Python running inside? Thanks in advance. On Sun, Sep 27, 2009 at 8:13 PM, edwithad <edwithad@hotmail.com> wrote: > I am sure you have not read a question this basic in some time, but I am > curious. Using Linux I open a terminal window and type: python. > > Does Bash Shell go away and to become a Python Shell, or is it still a Bash > Shell with Py...

inserting a newline character in a bash (or sh) shell
I am relatively new to scripting but have been a C++ programmer for years. I've been looking for a way to do this in a fancy grep script I'm developing (ironically to save me time..) and I can't find anything with google that helps. I've tried FAQs, local experts, etc but so far nothing has worked. I'll keep this short, in case it's stupid. This is what I want to do in a script, insert a newline between two variables for text output: output="$var1 \n $var2" However the newline insertion doesn't work and I get something like: string1 \n string2 I want ...

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

Python as a default shell, replacement of bash, sh, cmd ?
Has it been considered to add shell features to python, such that it can be used as a default shell, as a replacement for bash, etc. I'm sure everyone would agree that doing this would make the terminal very powerful. What are your views on this? Have a look at IPython (http://ipython.org/). It can interact with the normal shell very well. Laszlo On Sat, Feb 18, 2012 at 19:58, SherjilOzair <sherjilozair@gmail.com> wrote: > Has it been considered to add shell features to python, such that it can be used as a default shell, as a replacement for bash, etc. > > I'm sure...

What is the Expect equivalent to the Unix shell echo "...\c"
#!/usr/local/bin/expect -- set ifil [open "y" r] set ofil [open "TestResult.log" w] puts "[exec clear]" puts "\nWorking ..." puts "Please standby .\n" while { [gets $ifil line] >=0 } { log_user 1 puts ".\t" <<<<<<<<<<<<<<<<<<<<< I would like to print "."'s on the screen to let the useer know that the script is still working .... basically the equivalent of the Unix echo \c. log_user 0 spawn telnet expect "telnet>" send "open $line\n"...

how easily can glob.glob say Unix bash `echo *`
http://wiki.python.org/moin/glob now mentions: The glob module lists names in folders that match Unix shell patterns. If the elemental function emulating Unix bash `echo *` really is missing from the 2.5 Python batteries included, then here's a brief clear way of adding that function to Python. Mind you, to date I've never written an elemental function for Python that I haven't eventually found well-buried somewhere, e.g., os.urandom. P.S. glob.glob bash and glob.glob rm have no hits yet, said Google Groups. ...

bash/sh shell scripts and line-based list expansion
[sorry - there's probably a better linux group to ask this in] is there a way in bash/sh to get for loops to iterate a single line at a time, rather than a single word? In other words, given something like: for i in `cat /tmp/foo`; do echo ${i}; done ....the default behaviour is for the variable i to be set to each "word" (i.e. something separated by whitespace) from the file /tmp/foo for each loop iteration. What I need is for the i variable to be set to each line in turn. I thought that setting bash's IFS variable to just a newline (rather than t...

Web resources about - echo difference in sh and bash - comp.unix.shell

Difference - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

3Q EARNINGS: Mark Zuckerberg on the Differences Between Messenger and WhatsApp
When Facebook announced its intent to acquire cross-platform messaging application WhatsApp in February, questions surfaced on whether WhatsApp ...

Facebook Tips: What’s the Difference between Top News and Most Recent?
The following is part of our series, Facebook Tips, which answers some of the most commonly asked questions about using Facebook. While we hope ...

The Floral Society opens a bar with a difference in Kingston
It's Canberra's newest bar but it's less drinks and more daisy.

iOS App Store brings in 75% more revenue than Play Store despite difference in downloads
... app store. The iOS App Store now earns 75 percent more revenue than the Google Play Store. This is an increase over the 70 percent difference ...

Sprint cuts customer service jobs, says customers won’t notice a difference
Sprint is reportedly cutting 2,500 jobs, including 2,000 in customer service, but the company told Ars that subscribers won't actually see any ...

"I see little difference between a drone hovering near my window, and someone standing across the street ...
"Both can peer into my office. But I may be in the minority here. When I mentioned it to my wife, she was outraged by the intrusion and briefly ...

The difference between billionaires and the rest of us - Business Insider Deutschland
A quote from the late Apple founder sheds light on the "billionaire mentality."

It’s Mardi Gras: Celebrate Both Creole & Cajun But Know The Difference
... was in a Sazerac cocktail and how tabasco peppers were harvested. Above all, the most enlightening topic for me was to learn the real differences ...

Preview: Making a Difference - 60 Minutes Videos - CBS News
On a special edition of "60 Minutes Presents," the news magazine takes a look at Americans from all walks of life who are making a difference. ...

Resources last updated: 2/3/2016 6:16:48 AM