15

I have a series of commands a,b,c which I am chaining together with &&: a && b && c.

I want to catch the output of all the commands to both stdout and stderr. a && b && c 2>&1 > capture_file only captures the output from the c command.

2 Answers 2

15
{ a && b && c; } >capture_file 2>&1

Note the order of redirections: you have to redirect stdout first.

3
  • Why stdout first?
    – TLOlczyk
    Aug 18, 2015 at 2:58
  • @TLOlczyk 2>&1 means duplicating the destination of stdout(1) to stderr(2), so it would be useless unless you've set up stdout redirection beforehand.
    – yaegashi
    Aug 18, 2015 at 7:57
  • 2
    @TLOlczyk It doesn't have to be stdout first - you could do stderr first: 2>capture_file >&2 (but don't, because that's not idiomatic). The important thing is that you redirect one of them to file before duplicating that stream to the other. Aug 19, 2015 at 8:29
3

Apart from using command grouping {} you can also run the commands in a subshell and capture the output (and/or error) at once :

( a && b && c ) >file.txt 2>&1

Example :

$ ( echo foo && echo bar && echo baz && echos foo ) >check 2>&1

$ cat check
foo
bar
baz
No command 'echos' found, did you mean:
 Command 'echo' from package 'coreutils' (main)
echos: command not found

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .