leafsummer
7/8/2016 - 2:24 AM

cut string by semicolon to arrary in bash

cut string by semicolon to arrary in bash

#!/bin/bash
str="item1;item2;item3"
array=($(echo $str|tr ',' ' '|tr -s ' '))  #bash的方法
echo ${array[1]}
#item2
for s in ${arry[@]}; do echo "$s"; done
#item1 item2 item3

#or use this method
OLD_IFS="$IFS" 
IFS="," 
array=($str) 
IFS="$OLD_IFS" 
echo ${array[1]}
#item2
for s in ${arry[@]}; do echo "$s"; done
#item1 item2 item3