Entry Level
Entry level: strings like 1, 2, 3, 4, and 5
#! /Bin/bashvar = "1, 2, 3, 4, 5" Var =$ {var //,/} For I in $ var; do echo $ I; done
In this way, the results can be output.
Shap level
In the preceding example, the split string cannot contain spaces, for example, ", 9 ,".
#! /Bin/bashvar = "1, 2, 3, 4 9, 5" for (I = 1; I <= 4; I ++) do echo $ var | cut-d ", "-F $ idone
Here we will explain the small part of cut, which is similar to awk. The-D parameter is a separator and the-F parameter specifies the position.
Depth level
The disadvantage of the above example is that we need to calculate the number of "," in advance.
#! /Bin/bashvar = "1, 2, 3, 4 9, 5" I = 1 while (1 = 1); Do TMP = 'echo $ var | cut-d ", "-F $ I 'If [" $ tmp "=" "]; then break; else echo $ TMP; (I ++) fidone
This method is more complex, but more universal.
Shell string segmentation