(Sorry, long explanation)
Yes, the IFS
variable in while IFS=" " read; do …
has no effect on the rest of the code.
Let's first precise that the shell command line features two different kinds of variables:
- shell variables (which only exist within a shell, and are local to the shell)
- environment variables, which exist for every process. Those are usually preserved upon
fork()
and exec()
, so child processes inherit them.
When you call a command with:
A=foo B=bar command
the command is executed in within an environment where (environment) variable A
is set to foo
and B
is set to bar
. But with this command line, the current shell variables A
and B
are are left unchanged.
This is different from:
A=foo; B=bar; command
Here, shell variables A
and B
are defined and the command is run without environment variables A
and B
defined. Values of A
and B
are unaccessible from command
.
However, if some shell variables are export
-ed, the corresponding environment variables are synchronized with their respective shell variables. Example:
export A
export B
A=foo; B=bar; command
With this code, both shell variables and the shell environment variables are set to foo
and bar
. Since environment variables are inherited by sub-processes, command
will be able to access their values.
To jump back to your original question, in:
IFS='a' read
only read
is affected. And in fact, in this case, read
doesn't care about the value of the IFS
variable. It uses IFS
only when you ask the line to be split (and stored in several variables), like in:
echo "a : b : c" | IFS=":" read i j k; \
printf "i is '%s', j is '%s', k is '%s'" "$i" "$j" "$k"
IFS
is not used by read
unless it is called with arguments. (Edit: This is not exactly true: whitespace characters, i.e. space and tab, present in IFS
are always ignored at the beginning/end of the input line. )
I find very useful the following readline commands
history-search-backward,
history-search-forward
(be aware they are different from the usual reverse-search-history
, forward-search-history
, tied to Ctrl-R, Ctrl-S).
I have these commands associated to Ctrl-Up and Ctrl-Down putting the following lines into ~/.inputrc
:
"\e[1;5A": history-search-backward
"\e[1;5B": history-search-forward
How they work: write few chars of the beginning of the command, press Ctrl-Up and the next older command starting with that prefix will be shown, press again to see the next, and so on. When you are satisfied, after possibly modifying the command, press Enter to execute.
Best Answer
The trap is that
sets the
IFS
for the whole shell environment outside the loop, whereasredefines it only for the
read
invocation (except in the Bourne shell). You can check that doing a loop likethen after such loop,
echo "blabalbla $IFS ooooooo"
printswhereas after
the
IFS
stays redefined: nowecho "blabalbla $IFS ooooooo"
printsSo if you use the second form, you have to remember to reset :
IFS=$' \t\n'
.The second part of this question has been merged here, so I've removed the related answer from here.