shell按分隔符截取字符串

1.
#!/bin/bash
string="hello,shell,haha"  
array=(${string//,/ })  
for var in ${array[@]}
do
   echo $var
done 
2.
#!/bin/bash
string="hello,shell,haha"
OLD_IFS="$IFS"
IFS=","
array=($string)
IFS="$OLD_IFS"
for var in ${array[@]}
do
   echo $var
done
3.awk方式
var1=`echo "hello world"|awk -F '|' '{print $1}'`
echo $var1
var2=`echo "hello world"|awk -F ' ' '{print $2}'`
echo $var2

 

你可能感兴趣的:(LINUX)