On Mon, Dec 17, 2012 at 08:43:45PM -0800, Philip Guenther wrote:
> On Mon, Dec 17, 2012 at 5:14 PM, sven falempin <sven.falem...@gmail.com> 
> wrote:
> > So much to just print ...
> >
> > so:
> > 1 echo is crap (not portable, not very usefull)
> > 2 print is doing echo job in ksh  print [-nprsu[n] | -R [-en]] [argument
> > ...] (but this is completly different on pengouinOS)
> > 3 printf is everywhere and works fine
> 
> Ah, misc@, how I miss you...
> 
> echo is perfectly safe and portable for printing, followed by a
> newline, a literal string that doesn't start with a minus sign.
> 
> That happens to be
> a) a *really* common need, and
> b) a task solved by the historical echo command.
> 
> If that's not what you need, you should be considering printf instead
> of writing a non-portable echo.

True.  Basically, in order to write portable schell scripts, I would
advise to use `echo' for common message printing without anything
fancy because it is very often implemented as a shell builtin command,
so it has very little overhead compared to printf(1).  Given this is
probably 99% of use case, this is great.

As long as you need to interpret special escape patterns, avoid printing
a newline, or anything else, go for printf(1).

One problem as you noted above is strings starting with a dash, some
basic echo versions (like OpenBSD and Solaris ISTR) will just print it
as is whereas more elaborated (and wrong IMHO) versions (Linux and
FreeBSD) will try to interpreted the options after the dash:

    jlh@morgoth:~$ echo -d test
    -d test
    jlh@morgoth:~$ echo -e test
    test

For this reason, if you really want portable code, use should use this
instead of a bare `echo':

myecho() {
        case "$1" in
        -*) a="$*"; printf '%s\n' "$a" ;;
        *) echo "$*" ;;
        esac
}


Regards,
-- 
Jeremie Le Hen

Scientists say the world is made up of Protons, Neutrons and Electrons.
They forgot to mention Morons.

Reply via email to