码迷,mamicode.com
首页 > 编程语言 > 详细

shell 字符串转数组

时间:2019-08-24 00:03:15      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:net   spl   mac   col   空格   字符   done   div   href   

 

#!/bin/bash
string="hello,shell,split,test" 
#将,替换为空格 
array=(${string//,/ })  
 
for var in ${array[@]}
do
   echo $var
done 

输出

bogon:conf macname$ ./test.sh 
hello
shell
split
test

 

还可以写成

#!/bin/bash
string="hello,shell,split,test"  
array=(`echo $string | tr ,  ` )  
 
for var in ${array[@]}
do
   echo $var
done 
 

或者

#!/bin/bash
string="hello,shell,split,test"  
 
#对IFS变量 进行替换处理
OLD_IFS="$IFS"
IFS=","
array=($string)
IFS="$OLD_IFS"
 
for var in ${array[@]}
do
   echo $var
done

 

 

 

参考:

https://blog.csdn.net/u010003835/article/details/80750003

 

shell 字符串转数组

标签:net   spl   mac   col   空格   字符   done   div   href   

原文地址:https://www.cnblogs.com/sea-stream/p/11403174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!