Home » Scripting and Automation » Bash / Shell Scripts » What is the meaning of IFS in Bash Shell Script ?

What is the meaning of IFS in Bash Shell Script ?

The full form of IFS is “Internal Field Separator” alternatively based on its functionality this is also called as “Input Field Separator”. Technically in bash scripts IFS is a very special shell variable which is used to separate the contents based on the default values of IFS Variable.

By default, IFS is assigned with three characters, “space” , “new line” and “tab”.

Default 1 – Space

Now, lets show how default value of IFS can be used when input is separated by space with simple bash script as,

$ vim ifs_default_space.sh
#!/bin/bash

helloString="Hello Lynxbee Visitors, Welcome here"

for getWord in $helloString
do
        echo "Seperated based on space from string : $getWord"
done

So, to understand in simple words if we have some input string as “Hello Lynxbee Visitors, Welcome here” then if we assign this string as input to for loop and try to read sub-strings, the input string will be separated based on “space” which is IFS one of the default value and display the output on terminal as,

Hello
Lynxbee
Visitors,
Welcome
Here

Now if you run the script as shown in below image, you can see the output as,

Default 2 – New Line

$ vim ifs_default_new_line.sh
#!/bin/bash

helloString="This
is
on
new
line
"

for getWord in $helloString
do
        echo "Seperated based on new lines : $getWord"
done

When we run this script, as can see the output separated based on new lines.

$ bash ifs_default_new_line.sh 
Seperated based on new lines : This
Seperated based on new lines : is
Seperated based on new lines : on
Seperated based on new lines : new
Seperated based on new lines : line

Default 3 – TAB

$ vim ifs_default_tab.sh
#!/bin/bash

helloString="This       is      on      new     tab"

for getWord in $helloString
do
        echo "Seperated based on new tabs : $getWord"
done

If we run this above script, we can now see the input string split based on tab which is third default value of IFS.

$ bash ifs_default_tab.sh 
Seperated based on new tabs : This
Seperated based on new tabs : is
Seperated based on new tabs : on
Seperated based on new tabs : new
Seperated based on new tabs : tab

Using Custom Character as separator with IFS

We seen above the default IFS values are space, new line and tab, but what if we need to separate the string based on colon or semicolon or some other characters… The Bash scripts supports this feature by allowing us to override the default IFS value with new characters.

In below example, we will override the IFS with semicolon and show how to split the input.

Note : As we are going to override IFS with some new value, we need to make sure this also can be reset to default after our work is done, so if the script is very big it can continue its functionality in default way.

$ vim ifs_custom.sh
#!/bin/bash

helloString="This:is:joined:using:colon"

#since we are going to override default IFS value, we will need to restore
#this to original value once our custom work is done
#hence save original values as BACKUP_IFS
BACKUP_IFS=$IFS

#override default IFS
IFS=":"

for getWord in $helloString
do
        echo "Seperated based on new tabs : $getWord"
done

echo "-------------------------------------------------"

helloString="This;is;joined;using;semicolon"
IFS=";"
for getWord in $helloString
do
        echo "Seperated based on semicolon : $getWord"
done

echo "-------------------------------------------------"

#Now, as work is done, restore the IFS to original

IFS=$BACKUP_IFS

#Lets try to see now if restore worked
helloString="This is a hello world string"

for getWord in $helloString
do
        echo "Seperated based on space from string : $getWord"
done
$ bash ifs_custom.sh 
Seperated based on new tabs : This
Seperated based on new tabs : is
Seperated based on new tabs : joined
Seperated based on new tabs : using
Seperated based on new tabs : colon
-------------------------------------------------
Seperated based on semicolon : This
Seperated based on semicolon : is
Seperated based on semicolon : joined
Seperated based on semicolon : using
Seperated based on semicolon : semicolon
-------------------------------------------------
Seperated based on space from string : This
Seperated based on space from string : is
Seperated based on space from string : a
Seperated based on space from string : hello
Seperated based on space from string : world
Seperated based on space from string : string

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment