bug-bash
[Top][All Lists]
Advanced

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

Re: read problem


From: Dennis Williamson
Subject: Re: read problem
Date: Sat, 24 Sep 2022 11:21:53 -0500

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.

-- 
Visit serverfault.com to get your system administration questions answered.


reply via email to

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