0

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

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

readarray though works is not on the bsd machines i need it to be on = /
1

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[*]}

4 Comments

@JamesAndino Then, what means "not producing" ?
an array of all block devices recognized by the system
No the output of that echo with no quotes though looses information would still produce an output I can tell its working from as opposed to the out put of my command thats wronger then the just wrong input of my poor echo
@JamesAndino Isn't it that lsblk produces by default output in some tree format, maybe you wanted list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.