Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the syntax `while IFS= read line` documented?

Tags:

bash

shell

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.

like image 300
nachocab Avatar asked Jul 26 '11 13:07

nachocab


1 Answers

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.

like image 168
Jan Hudec Avatar answered Sep 26 '22 06:09

Jan Hudec