Peculiar behavior due to IFS


 
Thread Tools Search this Thread
Operating Systems Linux Peculiar behavior due to IFS
# 8  
Old 03-10-2013
Thanks Alister for the concept. I have 3 related queries please:
1)
Code:
$ IFS=ab
$ echo `echo cabcow`
$ c  cow
$ echo cabcow
$ cabcow

It's confirmed that SHELL parses or splits the fields based on IFS at command substitution or variable evaluation.
But how a command line is split into fields? Here IFS isn't considered!

2)
Code:
IFS=ab
echo $IFS | od -bc
040 012
       \n
echo "$IFS" | od -bc
141 142 012
  a    b    \n

why the space character displayed when not quoted?
3) The effect of double quotes :
Code:
echo `ls`
echo "`ls`"

The former command displays the filenames in a single line with a space in between 2 names.
The latter displays as the 'ls' output is.i,e. one name with newline character in each line.
May I conclude that when quoted, the command hasn't converted the newline character to a space which it was doing when not quoted. That means, here double quotes is interpreted by command also and not only by SHELL.
This User Gave Thanks to ravisingh For This Post:
# 9  
Old 03-10-2013
Ad 1.
Here the field is split in three parts:
- the "c" before the IFS character "a"
- The empty field between "a" and "b"
- And the string "cow" behind the "b"
These 3 fields are separated by spaces.

Ad 2.
The space is diplayed because in IFS there are two characters, "a" and "b". the first field is the empty string before that "a". The second field is the empty string between "a" and "b". The fields are split using a single space, which accounts for the space showing up in the od -c output. Since IFS acts as a field terminator rather then a separator, there is no third empty field after "b".

Ad 3. That is not correct. The output of the commands are input to the echo command. The first is unquoted, therefore it is split according to $IFS. Whereas the second one quoted and thus not split.

Last edited by Scrutinizer; 03-10-2013 at 11:17 AM..
# 10  
Old 03-10-2013
What a concept Scrutinizer and especially the 2nd one!!
Thanks a lot!
By the way, my 1st question is still left!
Code:
But how a command line is split into fields? Here IFS isn't considered!

By this I meant: (for ex.)
Code:
grep us file.txt

How the command line is parsed. Or, in other words, how grep understand what is 1st argument and the 2nd one? Here it isn't considering IFS!
# 11  
Old 03-10-2013
That is correct. IFS is only used after parameter expansion, command substitution and arithmetic expansion and with the read command.
So the shells always uses whitespace (space, tab, newline) as a separator between the various arguments and the command.

Last edited by Scrutinizer; 03-10-2013 at 02:00 PM..
# 12  
Old 03-10-2013
Quote:
Originally Posted by ravisingh
By this I meant: (for ex.)
Code:
grep us file.txt

How the command line is parsed. Or, in other words, how grep understand what is 1st argument and the 2nd one? Here it isn't considering IFS!
IFS is only used to split the results of unquoted expansions (such as $var, $(cmd)) and by the read command. The shell does not use it during its initial scan of the command line. During that early step, whitespace always delimits words (tokens, to be more precise), regardless of the value of IFS.

With regard to how grep finds its first argument, that's more involved. Once the shell has finished parsing the command line, it forks (or clones) itself. If all goes well, the new subshell calls one of the functions in the exec family with the command to run and a list of arguments to pass to it. This replaces the subshell with the command that was exec'd. The command's arguments are found in the array argv[], with the first argument at argv[1] (argv[0] is the command's name).

The details of creating the process and locating the list of arguments (and the environment) are overseen by the kernel and the c runtime.

Also, should you need to know every last detail of how your shell interprets command lines, you should read your shell's manual page in its entirety. There is also the POSIX standard's documentation, which will give you detailed knowledge of a common UNIX baseline:

POSIX
sh
Shell Command Language
exec family

Those links aren't intended to silence you (your questions are very welcome). I provide them only in case you are not aware of them.

Regards,
Alister

Last edited by alister; 03-10-2013 at 02:00 PM..
# 13  
Old 03-10-2013
Thanks Alister and Scrutinizer for the basics!
# 14  
Old 03-11-2013
Thank you too ravisingh for your good questions!

Last edited by Scrutinizer; 03-27-2013 at 02:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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) lspath() { OLDIFS="$IFS" IFS=: for DIR in $PATH ; do echo $DIR ; done IFS="$OLDIFS"... (8 Replies)
Discussion started by: scriptor
8 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. AIX

Peculiar permission problem

Scenario: Step 1. I'm logging into AIX server using user id called user1 Step 2. I'm traversing to home directory of user2 Note: This user2's home directory has the permissions drwxr-s--- Step 3. I'm issuing command pwd there. I'm getting the expected output. Step 4. I'm issuing the... (3 Replies)
Discussion started by: krishmaths
3 Replies

5. 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

6. 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

7. Shell Programming and Scripting

problem with IFS

hi, :) I set IFS=":" But when i try to echo $IFS,i am not getting any thing on the screen escept a blank line. any help pls. cheers RRK (11 Replies)
Discussion started by: ravi raj kumar
11 Replies

8. 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

9. Shell Programming and Scripting

a peculiar error with sftp

Whenever I sftped a particular gzipped file to a particular directory and then try to unzip it, I get Permission Denied error. With this file even I cannot do chmod. though the file permissions are -rw-r--r-- When same file I sftp to a different location I am able to gunzip it. Directory... (0 Replies)
Discussion started by: RishiPahuja
0 Replies

10. Linux

SUSE9.1 Peculiar Gigabit Lan performance.

I'm having some peculiar performance issues with my Gigabit Lan. I have some 100Mb devices so I can't do the necessary "jumbo Frame" tweaks for absolute optimum performance as I'd prevent them access. I'm getting appauling transfer rates sending files to the linux machine, around 10 Mbps 3%... (0 Replies)
Discussion started by: Mark Ward
0 Replies
Login or Register to Ask a Question