On Mon, Jan 13, 2025, at 3:03 AM, Félix Hauri wrote:
> Re-reading man page and this discussion, I thing either this variable is
> wrongly named "IFS", as `S` stand for "separator"

Okay.  Not like it's going to change.


> Btw, the command "mapfile" (readarray) seem more suitable** than "read" for
> splitting an array on delimiter. (** constant)

"Suitable" and "constant" don't mean remotely the same thing.
Perhaps you mean "consistent".


>    $ printf ' %-15s %-30s%s\n' String by\ Read{,Array};while IFS= read -r 
> line;
>        do
>          IFS=: read -r _k _v1 _v2 <<<"$line"
>          readarray -td: array < <(printf %s "$line");
>          k="${array[0]}" v1="${array[1]}";
>          IFS=:; v2=${array[*]:2}; IFS=$' \t\n';
>          printf " %-15s k=%-3s v1=%-4s v2=%-11s  k=%-3s v1=%-4s v2=%-11s\n" \
>                      "${line@Q}" "${_k@Q}" "${_v1@Q}" "${_v2@Q}" \
>                      "${k@Q}" "${v1@Q}" "${v2@Q}";
>      done < <(
>         printf '%s\n' \
>            k{:v1{:v2{:v3{:{:{:,},},},:{:{:,},},},:{:{:,},},},:{:{:,},},} )
>
>     String          by Read                       by ReadArray
>     'k:v1:v2:v3:::' k='k' v1='v1' v2='v2:v3:::'   k='k' v1='v1' v2='v2:v3::'  
>     'k:v1:v2:v3::'  k='k' v1='v1' v2='v2:v3::'    k='k' v1='v1' v2='v2:v3:'   
>     'k:v1:v2:v3:'   k='k' v1='v1' v2='v2:v3:'     k='k' v1='v1' v2='v2:v3'    
>     'k:v1:v2:v3'    k='k' v1='v1' v2='v2:v3'      k='k' v1='v1' v2='v2:v3'    
>     'k:v1:v2:::'    k='k' v1='v1' v2='v2:::'      k='k' v1='v1' v2='v2::'     
>     'k:v1:v2::'     k='k' v1='v1' v2='v2::'       k='k' v1='v1' v2='v2:'      
>     'k:v1:v2:'      k='k' v1='v1' v2='v2'         k='k' v1='v1' v2='v2'       
>     'k:v1:v2'       k='k' v1='v1' v2='v2'         k='k' v1='v1' v2='v2'       
>     'k:v1:::'       k='k' v1='v1' v2='::'         k='k' v1='v1' v2=':'        
>     'k:v1::'        k='k' v1='v1' v2=''           k='k' v1='v1' v2=''         
>     'k:v1:'         k='k' v1='v1' v2=''           k='k' v1='v1' v2=''         
>     'k:v1'          k='k' v1='v1' v2=''           k='k' v1='v1' v2=''         
>     'k:::'          k='k' v1=''   v2=''           k='k' v1=''   v2=''         
>     'k::'           k='k' v1=''   v2=''           k='k' v1=''   v2=''         
>     'k:'            k='k' v1=''   v2=''           k='k' v1=''   v2=''         
>     'k'             k='k' v1=''   v2=''           k='k' v1=''   v2=''         

What exactly is all of this supposed to demonstrate?  mapfile has
the same terminator-not-separator behavior everyone is kvetching
about:

        $ printf 'a:b:c:' | { mapfile -td:; declare -p MAPFILE; }
        declare -a MAPFILE=([0]="a" [1]="b" [2]="c")

Reading a line and splitting it into an array like this is what
read -a is for.


-- 
vq

Reply via email to