16

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.

5 Answers 5

23
> 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.

14

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.

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

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

2
  • Why the down vote? This answer conveys precisely the content of IFS in 3 different notations.
    – Alex Ryan
    Sep 7, 2017 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. Dec 28, 2017 at 23:49
1

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:

1
  • The echo and printf ones aren't super great with newlines... (just show blank lines)
    – rogerdpack
    Oct 15, 2021 at 18:49
0

I know this is an old post but another answer, and easy one that I have not seen posted yet is the following command

echo "${IFS@Q}" # which will result in the following (defaults)
' \t\n'

If you simply use

echo "$IFS" #Your result will be this

...an empty line showing the invisible characters which isnt much help.

Hope this helps Aloha

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .