2

What is a simple way to split a string and put into an array Here is my string

aa_bb__cc_dd

I would like to have them in an array like this:

array[0] = "aa"

array[1] = "bb"

array[2] = ""

array[3] = "cc"

array[4] = "dd"
HemChe
  • 2,249
  • 1
  • 21
  • 33
Samir
  • 21
  • 1
  • Look at this [solution][1], this exactly has what you're looking for. Just use `IFS='_' read -a array <<< "aa_bb__cc_dd"` [1]: http://stackoverflow.com/questions/10586153/bash-split-string-into-array – Máté Mar 26 '13 at 17:32

1 Answers1

5
var=aa_bb__cc_dd
oldIFS=$IFS
IFS=_
array=($var)
IFS=$oldIFS
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132