[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: printf '%s\n' "$@" versus <<< redirection
From: |
Kerin Millar |
Subject: |
Re: printf '%s\n' "$@" versus <<< redirection |
Date: |
Sun, 19 Feb 2023 18:34:16 +0000 |
On Sun, 19 Feb 2023 17:32:43 +0000
goncholden <goncholden@protonmail.com> wrote:
> ------- Original Message -------
> On Monday, February 20th, 2023 at 4:32 AM, Kerin Millar <kfm@plushkava.net>
> wrote:
>
>
> > On Sun, 19 Feb 2023 16:25:13 +0000
> > goncholden goncholden@protonmail.com wrote:
> >
> > > ------- Original Message -------
> > > On Sunday, February 19th, 2023 at 3:17 AM, Greg Wooledge
> > > greg@wooledge.org wrote:
> > >
> > > > On Sat, Feb 18, 2023 at 10:46:08AM +0000, goncholden wrote:
> > > >
> > > > > My intention is to use prinf line by line on arguments containing
> > > > > newlines. With a newline also introduced between arguments $1 $2 $3
> > > > > etc.
> > > >
> > > > This is quite unique. I don't believe I've ever seen someone try to
> > > > write a command where each argument is a group of lines, and all of
> > > > the groups of lines are supposed to be concatenated together to form
> > > > one bigger group of lines.
> > > >
> > > > For this goal, printf '%s\n' "$@" seems to be the correct choice.
> > > >
> > > > The <<< "$@" construct is nonsensical. Whatever it does (which is
> > > > pretty hard to predict, since it doesn't have a real definition), it
> > > > will not serve your goal.
> > > >
> > > > If you want to avoid a pipeline which would cause your processing loop
> > > > to run in a subshell, then your syntax of choice would be:
> > > >
> > > > while read ...
> > > > do
> > > > ...
> > > > done < <(printf '%s\n' "$@")
> > >
> > > I have found that nested loops are most clear Consequently, I have either
> > > this one
> > >
> > > Code:
> > >
> > > # Loop over arguments
> > > for arg in "$@"; do
> > > # Loop over lines
> > > printf '%s\n' "$arg" |
> > > while IFS= read -r vl ; do
> > > ...
> > > done
> > > done
> > >
> > > or this
> > >
> > > Code:
> > >
> > > # Loop over arguments
> > > for arg in "$@"; do
> > > # Loop over lines
> > > while IFS= read -r vl ; do
> > > ...
> > > done < <(printf '%s\n' "$arg")
> > > done
> >
> >
> > The use of a process substitution - as in your second example - is typical
> > in bash because, by having the while command be run in the initial shell,
> > it's immune to the issue of 'disappearing' variables. See
> > https://mywiki.wooledge.org/BashFAQ/024 for further details. Whether that
> > truly matters in your case depends on what "..." is exactly.
> >
> > --
> > Kerin Millar
>
> One purpose is to pass documentation information with colour designators for
> consecutive lines.
If it's only a matter of printing things to the standard output or standard
error, it won't matter which way you do it. Still, if in any doubt at all, use
a process substitution so that (in this case) printf is guaranteed to be the
only thing being run in a subshell.
--
Kerin Millar
Re: printf '%s\n' "$@" versus <<< redirection, alex xmb ratchev, 2023/02/18
- Re: printf '%s\n' "$@" versus <<< redirection, Kerin Millar, 2023/02/18
- Re: printf '%s\n' "$@" versus <<< redirection, alex xmb ratchev, 2023/02/18
- Re: printf '%s\n' "$@" versus <<< redirection, Mike Jonkmans, 2023/02/18
- Re: printf '%s\n' "$@" versus <<< redirection, Kerin Millar, 2023/02/18
- Re: printf '%s\n' "$@" versus <<< redirection, goncholden, 2023/02/19
- Re: printf '%s\n' "$@" versus <<< redirection, Kerin Millar, 2023/02/19
- Re: printf '%s\n' "$@" versus <<< redirection, Greg Wooledge, 2023/02/19
- Re: printf '%s\n' "$@" versus <<< redirection, Kerin Millar, 2023/02/19
- Re: printf '%s\n' "$@" versus <<< redirection, Greg Wooledge, 2023/02/19