bug-bash
[Top][All Lists]
Advanced

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

Re: read problem [SOLVED: works as documented]


From: kurt
Subject: Re: read problem [SOLVED: works as documented]
Date: Sat, 24 Sep 2022 19:32:29 +0200
User-agent: Evolution 3.44.1-0ubuntu1

Thanks for the clarification.

So 'ignoring whitespace' preceeds 'delimiter-logic' in the case of a single 
<tab>.

I compared the read and readarray sections and did not have a look at Word 
Splitting.

Sorry to bother.


Am Samstag, dem 24.09.2022 um 11:21 -0500 schrieb Dennis Williamson:
> 
> 
> On Sat, Sep 24, 2022 at 11:02 AM kurt <kurt@krzlprmpf.net> wrote:
> > Configuration Information [Automatically generated, do not change]:
> > Machine: x86_64
> > OS: linux-gnu
> > Compiler: gcc
> > Compilation CFLAGS: -g -O2 -flto=auto -ffat-lto-objects -flto=auto 
> > -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security 
> > -Wall 
> > uname output: Linux kurt-OptiPlex-7020 5.15.0-48-generic #54-Ubuntu SMP Fri 
> > Aug 26 13:26:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
> > Machine Type: x86_64-pc-linux-gnu
> > 
> > Bash Version: 5.1
> > Patch Level: 16
> > Release Status: release
> > 
> > Description:
> >         read into an array is inconsistent depending on the value of IFS 
> > (tested with ! and <tab>).
> >         as compared to readarray, which does the expected splitting count 
> > on both values.
> > 
> > Code to test the problem:
> > 
> >         declare -a aa adelim=("!" $'\t')
> >         declare s delim
> >         declare -i i j
> >         for((j=0;j<${#adelim[@]};j++)) do
> >           delim="${adelim[$j]}"
> >           printf -- 'delim is >%s<\n' "${delim}"
> >           s="."
> >           for((i=0;i<12;i++)) do
> >             if [[ $i -gt 4 && $i -lt 9 ]]; then
> >               s+="${delim}"
> >             else
> >               s+="${delim}$i"
> >             fi
> >           done
> >           printf -- '  >%s<\n' "$s"
> >           readarray -d "${delim}" aa <<< "$s"
> >           printf -- '  len readarray: %d\n' "${#aa[@]}"
> >           IFS="${delim}" read -a aa <<< "$s"
> >           printf -- '  len read     : %d\n' "${#aa[@]}"
> >         done
> > 
> > Output is:
> > 
> >         delim is >!<
> >           >.!0!1!2!3!4!!!!!9!10!11<
> >           len readarray: 13
> >           len read     : 13
> >         delim is >      <
> >           >.    0       1       2       3       4                           
> >             9       10      11<
> >           len readarray: 13
> >           len read     : 9
> > 
> > Expected for read would also be 13 when IFS is set to <tab>.
> > 
> > 
> 
> From man bash:
> 
>        The shell treats each character of IFS as a delimiter, and splits the 
> results of the other expansions into words on these characters.  If
>        IFS is unset, or its value is exactly <space><tab><newline>, the 
> default, then any sequence of IFS characters serves  to  delimit  words.
>        If IFS has a value other than the default, then sequences of the 
> whitespace characters space and tab are ignored at the beginning and end
>        of the word, as long as the whitespace character is in the value of 
> IFS (an IFS whitespace character).  Any character in IFS that is  not
>        IFS  whitespace,  along  with  any adjacent IFS whitespace characters, 
> delimits a field.  A sequence of IFS whitespace characters is also
>        treated as a delimiter.  If the value of IFS is null, no word 
> splitting occurs.
> 
> Note in particular the next-to-last sentence that begins with "A sequence". 
> IFS is used by read but not readarray which has its own delimiter argument. 
> See the manual sections for the two commands.
> 




reply via email to

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