Bash: How to tokenize a string variable?

Issue

If I have a string variable who’s value is "john is 17 years old" how do I tokenize this using spaces as the delimeter? Would I use awk?

Solution

Use the shell’s automatic tokenization of unquoted variables:

$ string="john is 17 years old"
$ for word in $string; do echo "$word"; done
john
is
17
years
old

If you want to change the delimiter you can set the $IFS variable, which stands for internal field separator. The default value of $IFS is " \t\n" (space, tab, newline).

$ string="john_is_17_years_old"
$ (IFS='_'; for word in $string; do echo "$word"; done)
john
is
17
years
old

(Note that in this second example I added parentheses around the second line. This creates a sub-shell so that the change to $IFS doesn’t persist. You generally don’t want to permanently change $IFS as it can wreak havoc on unsuspecting shell commands.)

Answered By – John Kugelman

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published