天天看點

shell字元串分割成數組

[email protected]host:~/xly# vim 1

#!/bin/bash

string="hello abc,shell,haha"

OLD_IFS="$IFS"

IFS=","

array=($string)

IFS="$OLD_IFS"

for var in ${array[@]}

do

echo $var

done

echo $string | awk '{split($0,arr,",");for(i in arr) print i,arr[i]}'

[email protected]:~/xly# sh 1 

hello

abc

shell

haha

1 hello abc

2 shell

3 haha

繼續閱讀