Bash – Setting a IFS for a bash script

bashenvironment-variablesread

The typical case is IFS= read.
The subject is very well explained in this thread:
why is setting a variable before a command legal in bash?
In order to have a complete picture, I'd be very grateful if you guys could explain why the following script doesn't work as (I) expected.
my_script:

#!/bin/bash

echo "$var1"
echo -n "$IFS" | xxd -p
echo "$var2"

exit 0

calling my_script:

var1=foo IFS= var2=bar my_script

output:

foo
20090a
bar

As you can see IFS is unchanged, still set as default.
How does read get the custom IFS when called as IFS= read?
Thank you in advance

Best Answer

The example does not set IFS within the script, because bash disallows importing IFS from the environment, according to a comment in variables.c:

  /* Don't allow IFS to be imported from the environment. */
  temp_var = bind_variable ("IFS", " \t\n", 0);
  setifs (temp_var); 

Built-in commands and non-script uses get the assignment to IFS, of course, but keep in mind that IFS only applies to word-splitting:

The shell treats each character of $IFS as a delimiter, and splits the results of the other expansions into words using these characters as field terminators. If IFS is unset, or its value is exactly <space><tab><newline>, the default, then sequences of <space>, <tab>, and <newline> at the beginning and end of the results of the previous expansions are ignored, and any sequence of IFS characters not at the beginning or end serves to delimit words. If IFS has a value other than the default, then sequences of the whitespace characters space and tab are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character). Any character in IFS that is not IFS whitespace, along with any adjacent IFS whitespace characters, delimits a field. A sequence of IFS whitespace characters is also treated as a delimiter. If the value of IFS is null, no word splitting occurs.

Related Question