Thursday, September 1, 2016

Bash: Easy Way to Tokenize Strings Separated by Space

Using the "set" command, it is possible to replace the position parameters with the one assigned. This can then be used to refer to individual elements of a string separated by space.

$ str="one two three"
$ set -- $str
$ echo $1
one
$ echo $2
two
$ echo $3
three

Reference: http://stackoverflow.com/questions/5382712/bash-how-to-tokenize-a-string-variable

No comments:

Post a Comment