Created
July 8, 2016 02:24
-
-
Save leafsummer/c8afcef6b5259dd9d52c5ae2f424941e to your computer and use it in GitHub Desktop.
cut string by semicolon to arrary in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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