bug-bash
[Top][All Lists]
Advanced

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

Re: IFS field splitting doesn't conform with POSIX


From: Robert Elz
Subject: Re: IFS field splitting doesn't conform with POSIX
Date: Sun, 02 Apr 2023 09:32:20 +0700

    Date:        Sat, 1 Apr 2023 19:44:10 -0400
    From:        Saint Michael <venefax@gmail.com>
    Message-ID:  
<CAC9cSOAA0g97fzuLuRXmee6EmRdUm7zPzk+U20YMvWbJyMVdng@mail.gmail.com>

  | The compelling reason is: I may not know how many values are stored in the
  | comma-separated list.

Others have told you you're wrong, but this is not any kind of compelling
reason - you simply give one more variable name than you expected to need
(than you would have used otherwise) and then all the extra fields that
you wanted the shell to ignore will be assigned to it - which you are free
to ignore if you like, or you can test to see if anything is there, and
issue an error message (or something) if more fields were given than you
were expecting.   Much better behaviour than the shell simply ignoring
data (silently).

  | GNU AWK, for instance, acts responsibly in the same exact situation:
  | line="a,b,c,d";awk -F, '{print $1}' <<< $line
  | a

awk is a different language with different rules, used in a different way.
Further, it isn't all that different really - you're only using $1, but
awk doesn't simply discard the other fields, they're there, called $2 $3 ...
There's even NF which tells you how many are there.

kre




reply via email to

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