[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Why variables are not set in a while-read loop?
From: |
Dan Douglas |
Subject: |
Re: [Help-bash] Why variables are not set in a while-read loop? |
Date: |
Sat, 21 Mar 2015 19:06:34 -0500 |
On Sat, Mar 21, 2015 at 4:39 PM, Peng Yu <address@hidden> wrote:
> Consequently, I should see $file with some value printed after the
> second loop but not the first loop.
`read' reads "lines", where bash extends the usual definition of a
"line" to mean a string terminated by the delimiter you specify.
`read' returns nonzero when it reaches EOF, not when it reads the
final delimiter.
If you have $'recordA\0recordB\0recordC\0', on the penultimate
iteration read will consume $'recordC\0' and return true. On the final
iteration read hits EOF before finding another delimiter and returns
false. Since there is no data following the last delimiter the
resulting empty string is assigned to `file'.
You do run the first loop in a subshell (unless lastpipe is enabled).
The second loop isn't in a subshell.
--
Dan Douglas