Peculiar behavior due to IFS


 
Thread Tools Search this Thread
Operating Systems Linux Peculiar behavior due to IFS
# 15  
Old 03-27-2013
Code:
$ cat emp.lst
Rob Mills
Jack Thompson
Steffi Blues

a=`cat emp.lst`
echo $a

The output of this echo shows the 3 names in a single line which is perfectly fine.

But my doubt is with the below:
Code:
echo "$a"

The output of this shows the 3 names in 3 lines.
Please see, now also I expected the same output because the variable a is already defined. When above I defined the variable "a", the command cat emp.lst was not quoted and hence variable "a" should not have accepted a newline character.
But this output says that the var. "a" has accepted the newline character.
How come this happen?
# 16  
Old 03-27-2013
What was IFS set to at the time?
# 17  
Old 03-27-2013
Quote:
Originally Posted by ravisingh
Code:
a=`cat emp.lst`

...

When above I defined the variable "a", the command cat emp.lst was not quoted and hence variable "a" should not have accepted a newline character.
Variable assignments bypass field splitting and pathname expansion (aka file globbing). IFS is irrelevant.

Since double quotes prevent field splitting and pathname expansion from occurring (the only sh parsing steps which can increase the number of elements in a command line), a="`cat emp.lst`" is equivalent to your unquoted version.

The newlines that you mention are in $a. The difference you observe is a result of quoting or not quoting echo's argument. The newlines are consumed by field splitting when the shell parses echo $a. The difference between the two echo commands is that in the quoted version, the shell is invoking echo with one argument, which contains three lines of names. In the unquoted version, the shell itself looks at the contents of $a, splits it on whitespace, consuming the newlines and (this is important) the spaces as well. echo is then invoked with 6 arguments, one for each word of the names. It is echo's job to then take its 6 arguments, join them with a single space between each, and print the result. In the quoted version, since echo is only passed a single argument, it does not add any space characters.

Note that command substitution always strips trailing newlines (not embedded). This has nothing to do with quoting, field splitting, IFS, nor variable assignment. It's how command substitution is designed. You may not have noticed that because echo, besides joining its arguments with a space character, appends a newline. If, however, your file has multiple newlines at the end, you will notice that when you echo the contents of a double-quoted $a, only a single newline is present (all trailing newlines were stripped by the command substitution but only one is added by echo).

Regards,
Alister

Last edited by alister; 03-27-2013 at 02:34 PM..
# 18  
Old 03-27-2013
Quote:
Originally Posted by alister
Variable assignments bypass field splitting and pathname expansion (aka file globbing). IFS is irrelevant.
Interesting disctinction. I've also noticed that IFS applies here:
Code:
set -- `cat file`

But not here:

Code:
set -- $(cat file)

# 19  
Old 03-27-2013
Quote:
Originally Posted by Corona688
Interesting disctinction.
It makes sense when you consider the effect that field splitting or pathname expansion could have. a=$1 could yield a=word1 word2 which would then execute an unintended command, word2, with a modification to its environment.

This bypassing of actions which can increase the number of tokens also occurs in the case statement: In case word in ..., word is not subject to field splitting and pathname expansion.


Quote:
Originally Posted by Corona688
Interesting disctinction. I've also noticed that IFS applies here:
Code:
set -- `cat file`

But not here:

Code:
set -- $(cat file)

I have not been able to reproduce such behavior nor have I been able to find any documentation implying that such behavior is intentional. Perhaps there's a bug in your shell? Can you specify the shell and demonstrate how you trigger such behavior?

I tried the following on bash 3.1.17 and did not observe any command substitution syntax-dependent difference in field splitting:
Code:
$ echo 1 2 3 > file

$ # With the default value of IFS, <space><tab><newline>
$ # Unquoted

$ set --; echo $#
0
$ set -- `cat file`; echo $#
3
$ set --; echo $#
0
$ set -- $(cat file); echo $#
3

$ # Quoted
$ set --; echo $#
0
$ set -- "`cat file`"; echo $#
1
$ set --; echo $#
0
$ set -- "$(cat file)"; echo $#
1

Regards,
Alister
# 20  
Old 03-27-2013
Interesting. I have bash 4, and thought I could depend on $( ) not splitting. Apparently not so.
# 21  
Old 03-28-2013
Rudic, got it.
Is it that the below variable assignment will work ?
Code:
$ a=`echo how are you`

I think as variable assignment is shell built-in, shell takes care that a is assigned the 3 words"how are you" even if the echo command is unquoted with double-quote.
---------------------------------------------------------------------------------
Also, 1 more point I had been thinking since long.
For example:
Code:
grep "$a" emp.lst

Who interprets double quote here: shell or grep or both?
According to me it's both.

Last edited by Scrutinizer; 03-28-2013 at 11:07 AM.. Reason: added slash to closing code tag
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