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.

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

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

  • Why stdout first?
    – TLOlczyk
    Aug 18 '15 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 '15 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 '15 at 8:29

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
No command 'echos' found, did you mean:
 Command 'echo' from package 'coreutils' (main)
echos: command not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.