Solved

what is IFS in a bash script

Posted on 2011-03-08
5
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINUX ZIP - UNCOMPRESS WINDOWS PATH 3 109
How do I send an email from raspberry pi at user login? 8 112
Debugging a shell script 3 36
AWK: Pythagoras script 6 37
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…
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…

733 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