Skip to content

Instantly share code, notes, and snippets.

@leafsummer
Created July 8, 2016 02:24
Show Gist options
  • Save leafsummer/c8afcef6b5259dd9d52c5ae2f424941e to your computer and use it in GitHub Desktop.
Save leafsummer/c8afcef6b5259dd9d52c5ae2f424941e to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment