Bash IFS


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash IFS
# 8  
Old 09-06-2012
Yes I ran echo "$*". Quite complicated this IFS!


---------- Post updated at 03:03 PM ---------- Previous update was at 02:47 PM ----------

So that means that the delimiter is always | after splitting? Splitting just splits according to either | or =, then arguments are delimited by |. Or I am messing things up at this point, and the only thing that matters is that arguments are now in $1, $2, ...? Why does it smash everything with |. Is that standard or is it because I used | as the first character in IFS? I am sure that if I use IFS="F=", instead of having |, I will have 'F' being displayed when the arguments are smashed together.
# 9  
Old 09-06-2012
It's actually quite simple, but works very differently from how people first imagine it Smilie

Try this.

Code:
$ VAR="a b c d"
$ printf "%s\n" 1 2 3 # Each argument on a different line

1
2
3

$ printf "%s\n" "$VAR" # IFS does not split here

a b c d

$ printf "%s\n" $VAR # IFS splits here

a
b
c
d

$

You see how the shell splits $VAR into 4 different arguments, but keeps "$VAR" as one argument.

Now try this:

Code:
$ set -- a b c d
$ printf "%s\n" $*

a
b
c
d

$ printf "%s\n" "$*" # IFS is space, so $1 $2 ... get spaces between

a b c d

$ IFS="|"
$ printf "%s\n" "$*" # With IFS="|", it puts pipes instead

a|b|c|d

$ printf "%s\n" $* # ...but if a|b|c|d isn't quoted, will split it into arguments.

a
b
c
d

$

# 10  
Old 09-06-2012
Quote:
Originally Posted by kristinu
Or I am messing things up at this point, and the only thing that matters is that arguments are now in $1, $2, ...?
Yes, exactly. IFS does not control what $1 $2 ... do -- it controls what an unquoted $ does. So we split once, setting the $1 $2 ... variables to exactly what we want, and can expect them to remain that way no matter what we do to IFS afterwards.

Quote:
Why does it smash everything with |. Is that standard or is it because I used | as the first character in IFS?
It's because it's the first character in IFS.

By default, that is space, so if you didn't change IFS, $* would give you "arg1 arg2 arg3 arg4" instead of "arg1|arg2|arg3|arg4".

See what I mean about it not being what you expected? You expected that $* gives you the same arguments you passed into the script. It never did, it always squashes $1 $2 ... together -- it just looked like your arguments because IFS defaults to space. (Well, space and tab and newline, but you get the idea.)


Quote:
I am sure that if I use IFS="F=", instead of having |, I will have 'F' being displayed when the arguments are smashed together.
Yes, exactly. I picked | because it's unlikely to be used in your arguments, but could have as easily used 'e', '@', '^', or any other ASCII character. Don't use * or ?, since the shell will try to glob on those. (like what happens when you do ls *.jpg)

It's important that it not be in your input, because IFS="F=" would split "--arg=Fast" into $1="--arg", and $2="ast" !

Last edited by Corona688; 09-06-2012 at 05:23 PM..
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 09-06-2012
I understand now and making sense of it all. Thanks again for all the information and examples. Smilie

---------- Post updated at 05:16 PM ---------- Previous update was at 03:25 PM ----------

I am now trying to update my script to use default values if the user does not supply values as command line arguments. Getting some problems when splitting for the individual values.

Here is the bash sequence

Code:
echo ""
IFS=$'/'
${val_xSrc:='-1.0/0/1.0'} # If val_xSrc is null or unset then it will be set to "-1.0/0/1.0".
unset IFS
echo "Using new method"
echo "val_xSrc = $val_xSrc"
xSrc1=${val_xSrc[$1]}  # default value 
xSrc2=${val_xSrc[$2]}  # default value
dxSrc=${val_xSrc[$3]}  # default value
echo "xsrc1 = $xSrc1"
echo "xsrc2 = $xSrc2"
echo "dxsrc = $dxSrc"
xSrcLen=${#val_xSrc[@]}
for (( i=0; i<${xSrcLen}; i++ ));
do
  echo "$i = ${val_xSrc[$i]}"
done
echo ""

# 12  
Old 09-06-2012
Using arrays in shell often makes a problem harder. It looks like you've been struggling, but it's simple enough the traditional way... Just read into the variables you want and prefix it with IFS to let it split on what you want. Three things in one shot and no arrays. And it'll work in any shell, not just BASH.

Code:
# If variable is blank, set it
[ -z "$val_xSrc" ] && val_xSrc="-1.0/0/1.0"

# Prefixing a variable to a command sets that variable for only that line
IFS="/" read xSrc1 xSrc2 dxSrc <<EOF
${val_xSrc}
EOF

echo "xSrc1 = $xSrc1"
echo "xSrc2 = $xSrc2"
echo "dxSrc = $dxSrc"

# 13  
Old 09-06-2012
I managed to do as follows. The problem was that -1.0/0/1.0 should be unquoted.

Code:
IFS="/"; ary=(${val_xSrc:=-1.0/0/1.0}); unset IFS
xSrc1=${ary[0]}; xSrc2=${ary[1]}; dxSrc=${ary[2]}

# 14  
Old 09-06-2012
You shouldn't be unsetting IFS, that could cause weird things later.

I'd reccomend doing things the way I showed you, instead.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash : More parameter expansion and IFS

I am trying to become more fluent with the interworking of bash and minimize the number of external calls. Sample Data. This will be the response of the snmp query. SNMPv2-MIB::sysName.0 = STRING: SomeHostName SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.9.1.1745... (5 Replies)
Discussion started by: sumguy
5 Replies

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

3. Shell Programming and Scripting

Changing IFS in bash function

I have a function in bash that takes arguments. does IFS work in a function or does it apply only to the main script? (1 Reply)
Discussion started by: kristinu
1 Replies

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

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

10. UNIX for Dummies Questions & Answers

IFS variable

How can I set the value for IFS variable (2 Replies)
Discussion started by: mahabunta
2 Replies
Login or Register to Ask a Question