Solved

what is IFS in a bash script

Posted on 2011-03-08
5
512 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 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

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 …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now