|

Bash – strings with multiple words inside them

Let’s assume we have a file containing lines such as this:

first last

one two

three four five

If we write a simple script to deal with each line in a turn, we would write something like this:

for i in `cat file`; do echo $i; done

This would echo, however, each word at its turn. If we want the whole line in this echo, we need to set BASH special variable: IFS.

Example:

export IFS=$'\n' ; for i in `cat file`; do echo $i; done

This would do the trick. We define the Internal Field Separator to be newline only, and not newline, spaces and tabs, as the default goes.

The credit for this piece of information I can easily give to this site. Thanks, guys.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.