James Andino
James Andino

Reputation: 25789

bash array with new line as IFS

why is this not producing the expected array of results

IFS=$'\n'; read -a list <<< `lsblk` ; echo ${list[*]};

Because I need to add to this question I have to say that I am a bit confused as to why

Upvotes: 0

Views: 406

Answers (2)

pmod
pmod

Reputation: 11007

Probably you need to use -l flag to get a list of devices, because by default lsblk output is in some tree view. (I don't have lsblk, but with lsusb I have expected result):

IFS=$'\n'; read -a list <<< `lsblk -l` ; echo ${list[*]};

You can also try the more native way of array creation below:

list=( $(lsblk -l) ); echo ${list[*]}

Upvotes: 1

janos
janos

Reputation: 124804

Maybe you're looking for this:

IFS=$'\n'; readarray list < <(lsblk)

And maybe it's better to check the result this way:

for i in ${list[@]}; do echo $i; done

The <(lsblk) there is called process substitution, you can read about it in the Process Substitution section of man bash.

On systems without readarray, you could do like this instead:

IFS=$'\n'; while read line; do list+=($line); done < <(lsblk)

Upvotes: 1

Related Questions