AFC
AFC

Reputation: 181

Bash not splitting a string into an array past the first index

I have a variable thats set to all sub directories in my current working dir, I did this by doing

directories=` ls -d -- */`

This returns the following to me "d1/ d2/" and I want to split this into an array by whitespace so I run the following code I found on stack overflow that seems to work for others but isn't working for me. The code is

IFS=' ' read -ra grArray <<< "$directories"

now this should split my string into two indices grArray[0] and grArray[1] which should be "d1/" and "d2/" respectively, but when I try to print both indices of the array I get "d1/" and the second array comes up as " "

If someone could point out what I'm missing here I would greatly appreciate it. Thank you.

Upvotes: 0

Views: 54

Answers (1)

chepner
chepner

Reputation: 531948

Don't use ls like this; just use a glob to set the array directly:

directories=( */ )

Upvotes: 5

Related Questions