How to capture the output of multiple commands?

I have a command that produces output belonging to different teams.

s=$(time dd if=<source> of=/dev/null bs=<number> count=<number> 2>&1)

$swill only contain output from the dd command. How can I get another variable that will contain the output from time?

+4
source share
3 answers

Try the following:

s=$({ time dd ... > /dev/null;} 2>&1)

Chepners' answer gave me inspiration, perhaps better:

s=$(exec 2>&1; time dd ... > /dev/null)

$() , . exec 2>&1;, stdout , , time , , , time. exec time, , , . exec, , forked-, .

, , , > /dev/null time /dev/null, , time .

+3

, .

.

.

s=$((time dd if=<source> of=/dev/null bs=<number> count=<number> 2>&1 | tail -n1) 2>&1)

$s, , .

+1

The built time bash- in is a special built-in module that records the standard error of the current shell, so you cannot just redirect your output. Instead, you will need to run it in a subshell, the standard error of which is already redirected. This makes the "real" command also run in the subshell, so you cannot just assign your output to a variable, as this variable will disappear with the subshell. Try the following:

s=$( exec 2>time.txt; time echo foo )
t=$(< time.txt)
+1
source

All Articles