linux 字串轉換成陣列
阿新 • • 發佈:2019-02-07
linux中陣列用小括號定義,所以轉換的時候只需要吧變數放在小括號中即可
1. 直接建立陣列
[[email protected] ~]# names=(wei hong rao)
[[email protected] ~]# echo ${#names[@]}
3
2.通過命令結果獲取陣列
[[email protected] ~]# names=($(echo 'wei hong rao') )
[[email protected] ~]# echo ${#names[@]}
3
3.通過轉換一個變數成為陣列
[[email protected] ~]# namestring="wei hong rao"
[ [email protected] ~]# names=($namestring)
[[email protected] ~]# echo ${#names[@]}
3
4. 指定分隔符號,用sed 替換就好
[[email protected] ~]# namestring="wei#hong#rao"
[[email protected] ~]# names=($(echo ${namestring}| sed 's/#/ /g'))
[[email protected] ~]# echo ${#names[@]}
3