7

I am currently running a shell script and I suspect the problem may be because I accidentally changed the default value of IFS which is a space to something else.

I want to check the current value of IFS, but I don't know how to go about finding it .

I am running my Ubuntu on VMWare.

10
> printf %q "$IFS"
' \t\n'

The %q format argument is used to interpolate the quoted variable $IFS and escapes control characters that would be interpreted by the shell.

Here you can see that IFS separates on spaces, tabs, and newlines.

9

As you probably already know, the default value of IFS is <space><tab><newline>. Using:

echo "$IFS"

you can probably deduce that there is a newline character and some other white space characters, but nothing sure.

To be sure about the exact value of theIFS variable, you can appeal to the help of cat command as follow:

echo "$IFS" | cat -ETv

which is equivalent with:

cat -ETv <<< "$IFS"

Sample output for the default value of IFS:

cat -ETv <<< "$IFS"
 ^I$
$

From the previous output you can deduce that there sure is one space character at the beginning and a newline character. But what is with the others strange characters. Let's look at the man cat:

   -E, --show-ends
          display $ at end of each line

   -T, --show-tabs
          display TAB characters as ^I

   -v, --show-nonprinting
          use ^ and M- notation, except for LFD (n.a. linefeed or newline character) and TAB

So, the ^I sequence from the above output means one TAB character and the other two $ characters means the end of the line.

1
$ echo -n "$IFS" | od -abc
0000000  sp  ht  nl
        040 011 012
             \t  \n
0000003

http://www.fvue.nl/wiki/Bash:_Show_IFS_value

  • Why the down vote? This answer conveys precisely the content of IFS in 3 different notations. – Alex Ryan Sep 7 '17 at 1:00
  • It would be nice with a short explanation of the od command and the options you suggest. I imagine the down vote may have been related to how-to-answer. – Jonas Dahlbæk Dec 28 '17 at 23:49
0

The command echo $IFS (note the absence of double quotes) may not show the value of IFS correctly because of word splitting.

For example:

IFS=:
echo $IFS  # shows a blank line

So, the right way to display IFS is:

echo "$IFS"

Other ways are:

set | grep -w IFS # shows IFS=$' \t\n'
printf '[%s]\n' "$IFS"

Related:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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