[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printf '%s\n' "$@" versus <<< redirection

From: Greg Wooledge
Subject: Re: printf '%s\n' "$@" versus <<< redirection
Date: Sun, 19 Feb 2023 08:51:09 -0500

On Sun, Feb 19, 2023 at 10:00:17AM +0000, goncholden wrote:
> theone()
> (
>     for arg in "$@"
>     do
>         while IFS='\n' read -r vl
>         do
>             printf '## vl: %s ##\n' "$vl"
>         done <<< "$arg"
>     done
> )

The IFS value you're using there contains a backslash and a lowercase n,
not a newline character.  Since you're passing a single variable name
to read, and since your IFS doesn't contain any whitespace characters,
that IFS does exactly nothing at all.

unicorn:~$ theone "no bananas"
## vl: no bananas ##

It doesn't even trim leading lowercase n's, because they're not

On the other hand, it also doesn't trim leading newlines (even though
those *are* whitespace), because you haven't included newline in your
IFS, but also because your read command stops reading at each newline,
and therefore newlines will never appear in the lines you're reading.

> var1="
> First Argument
> Another Line"
> var2="
> Second Argument
> Another Line"
> theone "$var1" "$var2"

If your intent was to strip the leading newlines from each of those
variables, you can see that it doesn't work:

unicorn:~$ theone $'\nFirst Argument\nAnother Line'
## vl:  ##
## vl: First Argument ##
## vl: Another Line ##

Even if you fixed your IFS (by writing IFS=$'\n' instead), it still
wouldn't work.  The first read command in the loop reads an empty
string, and applying IFS word splitting to an empty string will never
do anything.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]