Wes Miller
Wes Miller

Reputation: 2231

BASH: Send output of complex command to an array

NOTE FROM OP: Oops. My mistake. I happened to let grep hunt for something(s) non-existent. Of course I got no output. And yes, this is a dup of another question.

<><><><><><><><><><><><><><><><><><><><>

There are many answers on the web to (most of) this question. The "most of" part is my problem.

How do I capture the output of a command line into a bash array when the command line contains pipe chars, "|"?

 array=($(ps -ef | grep myproc | grep -v grep))

doesn't work. Neither does:

 array=(`ps -ef | grep myproc | grep -v grep`)

(those are backquotes in case your font mangles them).

And, can the given answer be use with array+= syntax?

Upvotes: 0

Views: 1679

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

array=($(ps -ef | grep myproc | grep -v grep))

works perfectly well. You can check it when you show the number of elements in your array

echo ${#array[*]}

or the complete array with

echo ${array[*]}

Upvotes: 1

Related Questions