what is IFS in a bash script

Posted on 2011-03-08
Medium Priority
Last Modified: 2012-05-11
pls explain  what this is doing and the role of IFS.
these are examples

Word Splitting
The results of parameter and arithmetic expansions, as well as command substitution, are subjected to word splitting if they were not quoted:

Word splitting is based on the value of the internal field separator variable, IFS. The default value of IFS contains the whitespace characters of space, tab, and newline (IFS=$' \t\n'). When IFS has its default value or is unset, any sequence of default IFS characters is read as a single delimiter.

$ var='   spaced
   out   '
$ sa $var

If IFS contains another character (or characters) as well as whitespace, then any sequence of whitespace characters plus that character will delimit a field, but every instance of a nonwhitespace character delimits a field:

S IFS=' :'
$ var="qwerty  : uiop :  :: er " ## :  :: delimits 2 empty fields
$ sa $var

If IFS contains only nonwhitespace characters, then every occurrence of every character in IFS delimits a field, and whitespace is preserved:

$ IFS=:
$ var="qwerty  : uiop :  :: er "
$ sa $var
:qwerty  :
: uiop :
:  :
: er :

Question by:TIMFOX123
LVL 10

Expert Comment

ID: 35074117
IFS specifies characters that a shell recognizes as word separators. It helps the shell to know how to split a string into separate words.
LVL 40

Expert Comment

ID: 35078666
IFS is input field separator. By default space is the filed separator. If you need to set it to something else, you use IFS.

LVL 10

Expert Comment

ID: 35079336
IFS is the space, tab, and newline characters by default
LVL 48

Accepted Solution

Tintin earned 2000 total points
ID: 35079827
Well, you've already posted an explanation, so perhaps some more examples would help.

Assume that file.dat has the following data

one two
three fou

Open in new window


You could read these fields with the following loop

while read f1 f2
   echo "field1=$f1"
   echo "field2=$f2"
done <file.dat

Open in new window

now assume file.dat has the following data


Open in new window

to be able to read and process it the same as the last set of code, you need to set IFS to a comma.

while read f1 f2
   echo "field1=$f1"
   echo "field2=$f2"
done <file.dat[

Open in new window


Author Closing Comment

ID: 35084418
thank you

great job all

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question