Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can change command substitution behavior in bash?

Tags:

bash

One of my bash shells is giving me different behavior than the others. I don't know what setting has changed, but check this out:

hersh@huey$ for f in $(echo a b c); do echo foo $f; done
foo a
foo b
foo c
hersh@huey$ logout
Connection to huey closed.
hersh@spf$ for f in $(echo a b c); do echo foo $f; done
foo a b c

On host huey, the "for" loop over did what I expected: it split on word boundaries. On host spf, the "for" loop did not split on word boundaries, it treated the whole thing as a single item. Similarly if I use $(ls) it will treat the whole big multi-line thing as a single item and print "foo" just in front of the first line.

On both hosts I'm running bash. In other bash shells on host spf it also works normally (splits on word boundaries). What could change that? Both are running the same version of bash (which is "4.2.25(1)-release (x86_64-pc-linux-gnu)"). The characters are the same, since I copy-pasted the for-loop command line with the mouse from the same source both times.

Thanks!

like image 336
Dave Avatar asked Oct 04 '22 01:10

Dave


1 Answers

Check your $IFS

mogul@linuxine:~$ for f in $(echo a b c); do echo foo $f; done
foo a
foo b
foo c
mogul@linuxine:~$ export IFS=SOMETHINGBAD
mogul@linuxine:~$ for f in $(echo a b c); do echo foo $f; done
foo a b c
mogul@linuxine:~$ 

and have a look at the manual over here bash Word Splitting

like image 147
mogul Avatar answered Oct 17 '22 17:10

mogul