Skip to content
Advertisement

IFS usage in bash

I have

JavaScript

I think I understand whats happening in 2nd and 4th for loops. But I do not understand why 1 was not printed in 1st and 3rd for loops. The whole idea of IFS is confusing to me in general.

Advertisement

Answer

When you say $var_name then 1 is interpreted as a separator and hence you see the string as cut into pieces.

JavaScript

When you add quotes around the variable, you demand for the absolute value of the variable without any processing.

JavaScript

Now coming to your question about 1st loop,

JavaScript

is equivalent to

JavaScript

‘i’ gets assigned “abc1def1gh1” and it prints out the value of variable ‘i’. This time ‘1’ is interpreted as a separator while printing ‘i’. This is what happened while running the loop:

JavaScript

Same thing happens in the 3rd loop.

Now if you want ‘1’ to be printed, then add quotes around $i in the loop. ie. change this:

JavaScript

to

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement