?
Solved

what is IFS in a bash script

Posted on 2011-03-08
5
Medium Priority
?
584 Views
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
:spaced:
:out:

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
:qwerty:
:uiop:
::
::
:er:

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 :


0
Comment
Question by:TIMFOX123
5 Comments
 
LVL 10

Expert Comment

by:NetExpert-Warszawa
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.
0
 
LVL 40

Expert Comment

by:omarfarid
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.

0
 
LVL 10

Expert Comment

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

Accepted Solution

by:
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

r

You could read these fields with the following loop

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

Open in new window



now assume file.dat has the following data

one,two
three,four

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.

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

Open in new window


0
 

Author Closing Comment

by:TIMFOX123
ID: 35084418
thank you

great job all
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
Suggested Courses

749 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