0
>export FOOBAR=foobar; IFS=b echo ${FOOBAR}

I was expecting to see

foo ar

but I see

foobar

Why?

2 Answers 2

2

The IFS hasnt yet taken effect. add another ";":

FOOBAR=foobar IFS=b; echo ${FOOBAR}

In man bash section SIMPLE COMMAND EXPANSION you can read (abbreviated):

  When a simple command is executed
  1. The words that the parser has marked as variable assignments (those preceding the command name) are saved for later processing.

  2. The words that are not variable assignments or redirections are expanded.

  3. ...

  4. The text after the = in each variable assignment ... [are] assigned to the variable.

so the IFS=b is done after expanding $FOOBAR.

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent. As-amended, no need for me to maintain my own answer here.
This can't be the answer. From a little further along in that same part of the man page: "Otherwise, the variables are added to the environment of the executed command and do not affect the current shell environment." Which means that the command being executed should see the IFS variable as was assigned, and wouldn't be seen after the command has been invoked. Otherwise, what would be the point of having this feature?
-1

[edit]I removed the technically incorrect answer.

http://tldp.org/LDP/abs/html/internalvariables.html "This variable determines how Bash recognizes fields, or word boundaries, when it interprets character strings."

4 Comments

All unquoted expansions are string-split (unless in [[ ]] or other special syntax suppressing that behavior); no loops needed.
I didn't say a loop was needed, I said, ie, which means, for example, a loop. However, with that said, I see the error in my reply.
Certainly, but you imply very strongly that echo $FOOBAR does not, in and of itself, string-split the value in FOOBAR before passing those values to echo. This implication is incorrect.
...by contrast, @meuh's answer -- preceding yours by two minutes -- is spot-on for accuracy; the only reason I added my own was that it's potentially lacking on completeness.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.