Skip to content
Advertisement

Bash: How to tokenize a string variable?

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?

Advertisement

Answer

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 " tn" (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.)

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