Why does every example I see have while IFS= read line and not while IFS=; read line?
I thought that name=value command might set a local variable but sentence="hello" echo $sentencedoesn't work, while sentence="hello"; echo $sentence does.
The:
name=value command
syntax sets the name to value for the command. In your example:
$ sentence="hello" echo $sentence
the $sentence is expanded by the calling shell, which does not see the setting. If you do
$ sentence="hello" sh -c 'echo $sentence'
(note the single quotes to have the $ expanded by the called shell) it will echo hello. And if you try
$ sentence="hello"; sh -c 'echo $sentence'
it will not echo anything, because sentence is set in the current shell, but not in the called one, since it was not exported. So
IFS=; read line
will not work, because read will not see the IFS setting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With