LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-13-2015, 09:11 AM   #1
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Rep: Reputation: Disabled
How does whitespace IFS function?


I just read from somewhere that if I had IFS set to be colon (i.e. 'IFS=:'), then the string "a:b:: c d "(2 spaces in between 'c' and 'd') would be separated into 4 fields, namely "a","b",""," c d ", which is quite obvious. However, it then said that if IFS was changed to be colon and space (i.e. 'IFS= :'), then the leading and trailing space of the string would be ignore and consecutive spaces would function as a separator as well; therefore, the same string, "a:b:: c d ", would be separated into 5 fields, "a","b","","c" and "d", but I think that it should be 6 fields, "a","b",""(this null character field is the one in between the two colons), ""(this null character field is the one in between the colon and the space, which is now a separator),"c","d". Which one is correct?
 
Old 10-13-2015, 12:40 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,760

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
Quote:
but I think that it should be
Sad to say, but irrelevant of what you may think, if you set IFS as described with that input you cannot argue with the output. So there is no question on which is correct.

So I think the part of man that explains what you see is in red below:
Code:
Word Splitting
       The shell scans the results of parameter expansion, command substitution, and arithmetic expansion that did not occur within double quotes for word splitting.

       The  shell  treats  each  character  of  IFS  as a delimiter, and splits the results of the other expansions into words using these characters as field terminators.  If IFS is unset, or its value is
       exactly <space><tab><newline>, the default, then sequences of <space>, <tab>, and <newline> at the beginning and end of the results of the previous expansions are ignored, and any  sequence  of  IFS
       characters not at the beginning or end serves to delimit words.  If IFS has a value other than the default, then sequences of the whitespace characters space and tab are ignored at the beginning and
       end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character).  Any character in IFS that is not IFS whitespace, along with any  adjacent  IFS  whitespace
       characters, delimits a field.  A sequence of IFS whitespace characters is also treated as a delimiter.  If the value of IFS is null, no word splitting occurs.
 
Old 10-14-2015, 02:21 AM   #3
glenjoker
LQ Newbie
 
Registered: Sep 2015
Distribution: Ubuntu 14.04
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Sad to say, but irrelevant of what you may think, if you set IFS as described with that input you cannot argue with the output. So there is no question on which is correct.
[/code]
This reminds me of my Electrodynamics professor who once said the same to me, but as I am very so often nowhere near uncovering or discerning the truth, what else can I do but have my superficial thoughts expressed and consult the experts so that I can learn from my mistakes.

Anyway, thanks for replying. It really clears up my doubts, and I really appreciate it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH:Rewrite a function using parameter expansion instead of changing IFS Mes9 Programming 7 09-26-2012 03:50 PM
Bash: when an empty IFS does not work like a default IFS (info) catkin Programming 13 04-19-2012 10:40 AM
IFS problem? sebelk Programming 5 05-17-2010 10:11 AM
Procmail and IFS cipher7836 Linux - Newbie 2 08-05-2009 12:13 PM
Ifs Gins Programming 2 07-18-2006 05:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration