Solved

what is IFS in a bash script

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 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

13 Experts available now in Live!

Get 1:1 Help Now