Skip to main content
1 of 4
a.smith
  • 745
  • 4
  • 15
  • 30

Shell script: How to assign each line of command output to values in an array?

When running a command i get 10 lines of output, i want to take lines 2-4-6-8-10 and put them into an array.

Every time i run my command the order changes so i need to do this in one go. I had tried running my command and picking out line 2, then running again and picking out line 4 etc but because the order changes this does not work:

value1=$(my_command |sed '2q;d')
value2=$(my_command |sed '4q;d')
value3=$(my_command |sed '6q;d')
value4=$(my_command |sed '8q;d')
value5=$(my_command |sed '10q;d')

MY_ARRAY=("${value1}" "${value2}" "${value3}" "${value4}" "${value5}")
a.smith
  • 745
  • 4
  • 15
  • 30