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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this community{ a && b && c; } >capture_file 2>&1
Note the order of redirections: you have to redirect stdout first.
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.
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
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