How to check the current value of IFS?
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.
> 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.
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.
$ echo -n "$IFS" | od -abc
0000000 sp ht nl
040 011 012
\t \n
0000003
http://www.fvue.nl/wiki/Bash:_Show_IFS_value
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:
- Word splitting in Bash with IFS set to a non-whitespace character
- Word Splitting - Greg's Wiki