Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was trying to change the IFS(Internal Field Separator) character to comma for a file processing, but to my surprise the IFS was un-changed.

Here's the order of commands and output :

mtk4@mtk-laptop:~$ echo .$IFS.
. .                                    # A space
mtk4@mtk-laptop:~$ IFS=","             # No error was thrown, no message
mtk4@mtk-laptop:~$ echo .$IFS.
. .                                    # Again echo'd it, but no change

Please let me know, what is wrong here?

share|improve this question
1  
For future reference, echo is not a reliable way to inspect unknown variable values. A much safer and more foolproof method would be something like printf %s "$var" | od -t c or your hex dumping tool of choice, to catch any unprintable ones. The default value of IFS should be space, tab, newline. –  jw013 Oct 18 '12 at 19:47
    
Interestingly, zsh prints IFS properly with echo, but bash doesn't. –  Kevin Oct 18 '12 at 20:47
2  
@Kevin That's because in zsh, variables's values do not undergo word splitting (this is zsh's most salient incompatibility with Bourne/POSIX shells). You'll see the same effect under emulate sh or with echo .$=IFS. –  Gilles Oct 18 '12 at 23:38

2 Answers 2

up vote 5 down vote accepted

IFS is also used as a separator when expanding a $variable with no quotes around it. In other words, you did change it, but, it got treated as a separator between the two dots. And, the echo command doesn't know what your $IFS is, and even if it did, it doesn't put it between its arguments for output, so you got a space there. Try echo ".$IFS."

share|improve this answer

Using bash you can use the builtin printf command with the %q format specifier to inspect the value of the IFS variable (see help printf).

printf '%q\n' "$IFS"    # $' \t\n'

# alternative, as already suggested by jw013
printf '%s' "$IFS" | od -A n -t x1 | xargs     # 20 09 0a
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.