Skip to content
Advertisement

Howto read result of command lspci each line as an element of array?

I’d like to have video card information from my target system, no matter what it is. There are two lines returned from my current target system, and I’d like to treat each line as an element of array. Using the code below, I got every word from lspci result rather than whole line which is what I need. Any idea?

myvideos=(`lspci | grep VGA`)
for video in ${myvideos[@]}
do
   echo "The $video"
done

The result returned from the code are:

The 00:02.0
The VGA
The compatible
The controller:
The Intel
The Corporation ....

What I need is :

00:02.0 VGA compatible controller: Intel Corporation

Thank you!

Advertisement

Answer

  1. use mapfile to capture the output into an array.

    mapfile -t myvideos < <(lspci | grep VGA)
    
  2. Absolutely crucial to use quotes on the array in the for loop

    for video in "${myvideos[@]}"; do ...
    
Advertisement