Why does reading act differently in bash and dash?
This is an attempt to figure out how a readable utility works across multiple shells. Found the difference which seems like a mistake to me.
The results are that the dash keeps trailing space with read:
dash: <a b >
bash: <a b>
In short: why does this code work differently in bash and dash read?
dash -c 'echo " a b " | { read var; echo "<$var>"; }'
bash -c 'echo " a b " | { read var; echo "<$var>"; }'
Here's an easier way to demonstrate your problem:
$ dash -c 'echo "a b " | { read var; echo "<$var>"; }'
<a b >
$ bash -c 'echo "a b " | { read var; echo "<$var>"; }'
<a b>
This only happens when there are two or more fields, for example "a b "
, and not when there is only one, for example "a "
.
Here's what POSIX says (emphasis mine):
If there are fewer vars than fields, the last var should be set to a value containing the following elements:
Field corresponding to the last var in the normal assignment sequence described above
Separator (s) that follow the field matching the last var
Remaining fields and their IFS trailing space delimiters are ignored
dash
does not ignore IFS whitespace, so it appears to violate POSIX.
Rule bash
.
source to share