Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #264647

Re: "echo" literally in sh scripts (was: why would "tr --complement --squeeze-repeats ..." append the substitution char once more? ...)

From Greg Wooledge <greg@wooledge.org>
Newsgroups linux.debian.user
Subject Re: "echo" literally in sh scripts (was: why would "tr --complement --squeeze-repeats ..." append the substitution char once more? ...)
Date 2023-12-11 18:00 +0100
Message-ID <HJRNo-cSlh-15@gated-at.bofh.it> (permalink)
References (5 earlier) <HJOmt-cQqs-1@gated-at.bofh.it> <HJOwa-cQtH-11@gated-at.bofh.it> <HJP8R-cQVR-3@gated-at.bofh.it> <HJPVg-cRbE-15@gated-at.bofh.it> <HJQeC-cRyf-17@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Mon, Dec 11, 2023 at 10:16:35AM -0500, Stefan Monnier wrote:
> > 1) Many implementations of echo will interpret parts of their argument(s),
> >    in addition to processing options like -n.  If you want to print a
> >    variable's contents to standard output without *any* interpretation,
> >    use printf.
> >
> >     printf %s "$myvar"
> >     printf '%s\n' "$myvar"
> 
> Interesting.  I used the following instead:
> 
>     bugit_echo () {
>         # POSIX `echo` has all kinds of "features" we don't want, such as
>         # handling of \c and -n.
>         cat <<ENDDOC
>     $*
>     ENDDOC
>     }

That requires an external command (one fork), plus whatever overhead is
used by the << implementation (temp file or pipe, depending on shell and
version).  It's not wrong, but an implementation using nothing but
builtins is usually preferable.

    echo() { printf '%s\n' "$*"; }

It's also worth mentioning that both of these rely on the expansion of $*
with a default or nearly-default IFS variable.  If you want it to work
when IFS may have been altered, you can do this in bash:

    echo() { local IFS=' '; printf '%s\n' "$*"; }

In sh, you'd need to fork a subshell:

    echo() { (IFS=' '; printf '%s\n' "$*"); }

Or if you're a golfer:

    echo() (IFS=' '; printf '%s\n' "$*")

I *really* dislike that syntax, but that's just me.  Some people use it.

Back to linux.debian.user | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Albretch Mueller <lbrtchx@gmail.com> - 2023-12-11 04:00 +0100
  Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Greg Wooledge <greg@wooledge.org> - 2023-12-11 04:40 +0100
    Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Albretch Mueller <lbrtchx@gmail.com> - 2023-12-11 09:10 +0100
      Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... <tomas@tuxteam.de> - 2023-12-11 09:40 +0100
        Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Albretch Mueller <lbrtchx@gmail.com> - 2023-12-11 12:30 +0100
          Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... tomas@tuxteam.de - 2023-12-11 13:00 +0100
          Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Greg Wooledge <greg@wooledge.org> - 2023-12-11 13:40 +0100
        Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Greg Wooledge <greg@wooledge.org> - 2023-12-11 13:50 +0100
          Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... <tomas@tuxteam.de> - 2023-12-11 14:20 +0100
            Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Greg Wooledge <greg@wooledge.org> - 2023-12-11 14:30 +0100
              Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Albretch Mueller <lbrtchx@gmail.com> - 2023-12-11 15:10 +0100
                Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Max Nikulin <manikulin@gmail.com> - 2023-12-11 16:00 +0100
                Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Greg Wooledge <greg@wooledge.org> - 2023-12-11 16:00 +0100
                Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... <tomas@tuxteam.de> - 2023-12-11 16:10 +0100
                "echo" literally in sh scripts (was: why would "tr --complement --squeeze-repeats ..." append the substitution char once more? ...) Stefan Monnier <monnier@iro.umontreal.ca> - 2023-12-11 16:20 +0100
                Re: "echo" literally in sh scripts (was: why would "tr --complement  --squeeze-repeats ..." append the substitution char once more? ...) Greg Wooledge <greg@wooledge.org> - 2023-12-11 18:00 +0100
                Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... Albretch Mueller <lbrtchx@gmail.com> - 2023-12-11 20:00 +0100
  Re: why would "tr --complement --squeeze-repeats ..." append the  substitution char once more? ... debian-user@howorth.org.uk - 2023-12-11 19:20 +0100

csiph-web