help-bash
[Top][All Lists]
Advanced

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

Re: Bash bug?


From: Greg Wooledge
Subject: Re: Bash bug?
Date: Tue, 24 Jan 2023 10:28:32 -0500

On Tue, Jan 24, 2023 at 03:03:48PM +0000, Leonid Isaev wrote:
> On Tue, Jan 24, 2023 at 09:21:47AM -0500, Greg Wooledge wrote:
> > If you want to send an input stream without adding a newline to it, you
> > can use < <(printf %s "$var") instead of <<< "$var".
> 
> BTW, is there any advantage of using mapfile vs read here? For example:
> -----8<-----
> I-akula-++-09:59-~-> IFS=" " read -ra a <<< "1 2 3"
> I-akula-++-10:00-~-> printf "x%sx\n" "${a[@]}"
> x1x
> x2x
> x3x
> ----->8-----

I'd say it depends on the input.  Given how obviously artificial this
input is, it's hard to say what it's standing in for.

Normally, I'd suggest you use mapfile when you've got multiple lines
of input, and each line can be arbitrarily long or complex.  Use IFS/read
when you've got a single line, and you're trying to "split" it into
an array of fields (like the over-simplistic way people try to parse a
CSV file, which is doomed to failure in any real-life case).

If you can show us a (worst case!) sample of your actual input, we can
suggest parsing approaches that would be suitable to that input.

By the way, when using IFS/read for parsing "CSV-like" input, beware of
the "empty fields at the end" pitfall.

https://mywiki.wooledge.org/BashPitfalls#pf47

Nothing is ever easy in bash.



reply via email to

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