[Top][All Lists]

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

`read' builtin does not take last record without trailing delimiter

From: Scott Mcdermott
Subject: `read' builtin does not take last record without trailing delimiter
Date: Sun, 24 Feb 2008 14:25:50 -0800
User-agent: Mutt/1.5.17 (2007-11-01)

Bash does not seem to take the last record if it has no
trailing delimiter:

    $ echo "0 1 2^3 4 5^6 7 8" |
      while read -a array -d ^
      do echo $array
    0 3

In this sense it does not behave like awk:

    $ echo "0 1 2^3 4 5^6 7 8" |
      awk -v RS=^ '{print $1}'
    0 3 6

This behavior is counter-intuitive.  It just throws away the
last line and it's never even put into the array.  I think
the whole intention of using read to split lines by
delimiter is always to read the last line even though it has
a trailing null or newline instead of a delimiter, just like
awk does.

IMO this is a bug but I'm sure this behavior is known and I
am wondering at its rationalization? I don't see any
arguments about it in the archives, this is just accepted?
It seems silly for a script to always have to take care to
add a delimiter even if the input doesn't have one, as is
extremely common (perhaps even universal).  At the very
least I think an option should be present to turn on this
behavior for the builtin, because I can see some cases where
one *would* want the behavior as implemented as well.

What do people think of this?



reply via email to

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