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: (sh/bash) How to check for a string matching -*? - comp.unix.shell ...One difference that springs to mind is: $ ksh -c 'echo $((010))' 10 $ sh -c 'echo $((010))' 8 -- Geoff ... (sh/bash) How to check for a string matching ... /bin/sh: Local variables in a function ? - comp.unix.solaris ...However, since nothing explicitly tells me any differences ... myfunc() { mylocal=1 myglobal=B echo ... (sh/bash) How to check for a string matching -*? - comp ... bash: how to flush the output from command `tee` - comp.unix.shell ...Below is my pipe line under bash(RHEL5): /path/to/my_script.sh 2>/dev/null | tee /path/to/log ... In that case, sh -c "echo x;sleep 1"|tee, I can't see any block ... the difference between two forms of logic and - comp.unix.shell ...... the pointed line, a error would come. What's the difference ... STACK%% *} Okay, so you are using ksh(1) (or bash(1)?) ... friebel@ifh.de> F,"dirs.sh; popd.sh; pushd.sh",,http ... pass arguments in bash - comp.unix.programmerHello, When I worked with bash script, I got stuck with a problem ... sub_test.sh $SAVED_ARGS #### sub_test.sh ##### #!/bin/sh while [ $# != 0 ]; do echo $1 ... password no echo in bash script - comp.unix.shellpassword no echo in bash script - comp.unix.shell Hi, Is it possible to create a script ... comp.unix.admin... to setup some environment variables in a script: #!/bin/sh echo ... when using the Matlab "unix" command I get a unix broken pipe ...The file being piped "veryLargeFile.txt " is large > 50000 lines the bash script "test.sh ... If other echo statements are added they all execute correctly and the script ... expect script to answer a question on behalf of the user - comp ...i have a simple bash script (ask.sh) which asks the user three questions when run ... password no echo in bash script - comp.unix.shell expect script to answer a ... how to simulate press "spacebar, enter, esc keys" at the unix ...... telnet to a cisco router and printout the "show run" as below: #!/bin/ksh ( echo ... Test regex in KSH - comp.unix.shell Test regex in KSH - comp.unix.shell (sh/bash ... Bash: strip file path and file extension from a file name in a ...Rainer Weikusat <rweikusat@mssgmbh.com> writes: [...] > [rw@sapphire]~ $echo ... (sh/bash) How to check for a string matching -*? - comp.unix.shell ... Bash: strip file ... Basic I/O Redirection Differences In Sh/Ksh, Bash and Zsh On Linux ...Now let's take a look at a how sh/ksh, bash ... host # truss echo hi &>FILE In bash, the &> notation is ... Basic I/O Redirection Differences In Sh/Ksh, Bash ... Internal Commands and Builtins#!/bin/bash # echo-params.sh # Call this script with a few command-line parameters. # For example: # sh echo-params.sh first second third fourth fifth params ... 7/27/2012 4:56:13 PM
|