Not able to understand IFS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not able to understand IFS
# 1  
Old 02-01-2013
Not able to understand IFS

Hi ,

i am in my initial learning phase of unix. i was going thru the function part.
below is the example which was there but i am not able to understand logic and the use of IFS(internal field separator)

Code:
lspath() {
OLDIFS="$IFS"
IFS=:
for DIR in $PATH ; do echo $DIR ; done
IFS="$OLDIFS"
}

the output is below. here i am failed to understand how the o/p is coming like below
and logic behind the IFS.

Code:
/sbin
/bin
/usr/bin
/usr/sbin
/opt/bin
/usr/ucb
/usr/ccs/bin
/usr/openwin/bin

regards,
scriptor
# 2  
Old 02-01-2013

man bash


Code:
 
     IFS  The Internal Field Separator  that  is  used  for  word
          splitting after expansion and to split lines into words
          with the read builtin command.  The  default  value  is
          ``<space><tab><newline>''.

# 3  
Old 02-01-2013
The shell you are using always tries to "expand" your command line, i.e. separates it into words, which in turn it then treats according to different rules, cf. man page of your shell.
In order to separate the line, the shell needs to know where words start and end. This is where IFS comes into play. Usually set to <space>,<tab>,<newline> characters for normal command lines, you can set it to what you want by assigning a new value. Doing this, it's safe to save the old value(s). In your example, the PATH environment variable looks like sth like /sbin:/bin:/usr/bin:/usr/sbin... (must be a root user), a colon separated list. IFS=: tells the shell to separate exactly there when supplying parameters to the for construct.
# 4  
Old 02-01-2013
hi RudicC

is it necessary to use environmental variable in case of IFS .
why we are this
PHP Code:
OLDIFS="$IFS
is IFS is itself a environmental variable ? i think it is not but i am not sure
# 5  
Old 02-01-2013
Quote:
Originally Posted by scriptor
hi RudicC

is it necessary to use environmental variable in case of IFS .
why we are this
PHP Code:
OLDIFS="$IFS
is IFS is itself a environmental variable ? i think it is not but i am not sure
When a shell starts, the IFS variable may be inherited from the environment or initialized by the shell to <space>, <tab>, and <newline> in that order. If IFS is unset (which is very different from setting IFS to an empty string), the shell and the read speical built-in utility split fields as if IFS had been set to <space>, <tab>, and <newline> in that order.

When you need to use non-default word splitting (e.g., using : as the field separator when grabbing directories out of $PATH), saving the old value and restoring it after you have completed the need for special word splitting actions is just good programming practice. In the example code given, OLDIFS is used to save the old value of IFS before setting IFS to :. After $PATH is split, setting IFS to OLDIFS restores its original value (as long as it hadn't been unset).

Whether or not IFS is exported doesn't matter for this discussion.
# 6  
Old 02-01-2013
Hi RudicC,

thx a lot you cleared most of my doubt.
however below script is not working.

Code:
#!/bin/sh
x="dgg dgf dg"
y="$IFS"
IFS=:
for t in $x; do echo $x; done
IFS="$y

below is the o/p which i am getting.

Code:
$ ./fun3
dgg dgf dg
$


Last edited by scriptor; 02-01-2013 at 05:27 AM.. Reason: removing typo mistake
# 7  
Old 02-01-2013
What did you expect after reading Don's and my explanations (or - maybe - not?)? You have a space separated input and try to separate it by colons. The shell will of course use $x as one single lump of chars, assign it to t unmodified, and echo it as is.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remote while IFS

Hello masters of scripting, I've been working to develop some basic monitoring scripts. I have solved one problem, but want to know how to solve the other. I have a script that runs locally to create an output file with the Linux system kernel paramters, preceeded by the system name: ... (2 Replies)
Discussion started by: LinuxRacr
2 Replies

2. Shell Programming and Scripting

Bash IFS

I am using bash and resetting IFS as below when reading the command line arguments. I do this so I can call my script as in Ex1. Ex1: ./synt2d-ray3dmod.bash --xsrc=12/20/30 This allows me to split both sides so that when I do "shift" I can get 12/20/30 What I do not understand is... (21 Replies)
Discussion started by: kristinu
21 Replies

3. Shell Programming and Scripting

Nested ifs

hi I keep getting an error with this nested if statement and am getting the error unexpected end of file, can anyone help me as to why this wont execute? #!/bin/bash #script to check wether the -i -v statements run correctly removeFile () { mv $1 $HOME/deleted }... (3 Replies)
Discussion started by: somersetdan
3 Replies

4. Shell Programming and Scripting

While loop and IFS?

Hi, while ; do echo "Please enter " read enter yyyy=${enter:0:4} mm=${enter:5:2} dd=${enter:8:2} result=`validateDate $yyyy $mm $dd` When does the loop keeping repeating till?? till 1 is equal to 1? what does this mean "${enter:0:4}" .The 0 and 4 part?? ... (3 Replies)
Discussion started by: sid22
3 Replies

5. Shell Programming and Scripting

How to use IFS in this scenario?

Given the scenario like this, if at all if have to use IFS on the below given example, how it should be used. IFS=/ eg: /xyz/123/348/file1 I want to use the last slash /file1 . So can anyone, suggest me how to pick the last "/" as a IFS. (4 Replies)
Discussion started by: raghunsi
4 Replies

6. Shell Programming and Scripting

read and IFS

Hi, This is out of curiosity: I wanted to extract year, month and date from a variable, and thought that combining read and IFS would help, but this doesn't work: echo "2010 10 12" | read y m d I could extract the parts of the date when separated by a -, and setting IFS in a subshell: ... (3 Replies)
Discussion started by: raphinou
3 Replies

7. Shell Programming and Scripting

while loop with 3 ifs

im messing up somehwere...and can't seem to clean up the script...for it to work objectives: 1. check for today's file, and sleep 30 secs between retries 2. only allow 5 tries before script should fail. 3. if today's file found, wait 30 seconds for it to process.. code: count=0... (8 Replies)
Discussion started by: sigh2010
8 Replies

8. Shell Programming and Scripting

regarding IFS=

hi i am a learner can some explain "export IFS=$(echo "\n\t\a")" i am not able to understand the functionality please help thanks Satya (1 Reply)
Discussion started by: Satyak
1 Replies

9. UNIX for Dummies Questions & Answers

Help on IFS command!

Hi! I am working in korn shell. I want to reset the dimiliter for the set command to "|" but instead of a command prompt return I am getting something as below After issuing the command I am getting this....as if the shell is expecting something else. Can anybody suggest what's the problem. ... (2 Replies)
Discussion started by: udiptya
2 Replies

10. UNIX for Dummies Questions & Answers

the IFS variable

Hi all, Ok os heres my situation. I have created a database style program that stores a persons info (name,address,phone number etc.) in a file ("database"). after i read in all the values above, i assign them to a line variable: line="$name^$address^$phonenum" >> phonebuk as you can see... (1 Reply)
Discussion started by: djt0506
1 Replies
Login or Register to Ask a Question