redirect stdout and stderr in a shell script
February 14th, 2008 by Lawrence David
to send the standard output and standard error streams from your shell script somewhere, place the following at the top of your script:
#!/bin/bash
exec &>Â output.file;
[the rest of your code]
Something very helpful is to redirect stderr in stdin,
so you can pipe to grep or whatever:
zsh$ cat /foo 2>&1 | grep ‘No such file’
cat: /foo: No such file or directory
If you want to keep only stderr you can do :
zsh$ cat /foo 2>&1 1> /dev/null | grep ‘No such file’
cat: /foo: No such file or directory