bash - split command output into array of lines -


here's bash function command's output parameter , return array of output lines.

function get_lines {      while read -r line;          echo $line     done <<< $1 }  sessions=`loginctl list-sessions` get_lines "$sessions" 

actual output of loginctl list-sessions is:

   session        uid user             seat                     c2       1000 asif             seat0                    c7       1002 sadia            seat0            

but while loop runs once printing output in single line. how can array of lines , return it?

you use readarray , avoid get_lines function:

readarray sessions < <(loginctl --no-legend list-sessions) 

this create array sessions each line of output of command mapped element of array.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -