Where is the syntax `while IFS= read line` documented?
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 l开发者_运维知识库ocal variable but sentence="hello" echo $sentence
doesn'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.
Prefixing a command with parameter assignment affects the environment of the command being executed.
In man bash
:
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described above in PARAMETERS. These assignment statements affect only the environment seen by that command.
In your example: sentence="hello" echo $sentence
, sentence
will be in the environment of the echo
command (try this to show yourself: sentence=HI env | fgrep sentence
), but not in the shell from which you are calling it, which would be required to pass it as an argument to echo
as you are attempting.
精彩评论