Loop the way you want it – Bash – IFS

You have a text fine containing several lines of text. You want to loop through each line so write a for loop in your bash script saying,

for lines in `cat somefile`;do
#do something

But wait a minute, your code is now processing word by word and not line by line. Whats happening? It is basically using space as the separator by default. So what do you have to do now. Use line break(\n) as your separator. How do you do that, IFS. IFS stands for Internal Field Separator.

So now if I add the line,
to my script before my for loop. Bash would now start looping through the lines rather than words.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s