tags:

views:

914

answers:

1

Is there anyway to pipe the output of a command which lists a bunch of numbers (each number in a separate line) and initialize a bash array with those numbers ?

Details: This lists 3 changelist numbers which have been submitted in the following date range. The output is then piped to "cut" to filter it further to get just the changelist numbers.

p4 changes -m 3 -u edk -s submitted @2009/05/01,@now | cut -d ' ' -f 2

E.g. :

422311

543210

444000

How is it possible to store this list in a bash array ?

+6  A: 

You can execute the command under ticks and set the Array like,

ARRAY=(`command`)

Alternatively, you can save the output of the command to a file and cat it similarly,

command > file.txt
ARRAY=(`cat file.txt`)

Or, simply one of the following forms suggested in the comments below,

ARRAY=(`< file.txt`)
ARRAY=($(<file.txt))
nik
Useless use of cat: `<file.txt` does the same.
ephemient
+1 @ephemient, you are right about that.
nik
fixed the answer for future reference.
nik
Whenever possible, avoid the use of back quotes. This is more readable and can work with nesting without awkward quoting: ARRAY=($(command)) or ARRAY=($(< file.txt))
Dennis Williamson