Capture Error of IO Redirection
Most BASH programmers know how to redirect stderr of a command. But, what if the IO redirection itself failed? Though it is not frequently used, capturing possible error message of IO redirection sometimes can be useful. This can be done by group the whole commandline (or pipe) and redirect it as shown below.
$ date > log_file/doet/not/exist 2>&1 -bash: file/doet/not/exist: No such file or directory $ msg=$( (date > log_file/doet/not/exist 2>&1) 2>&1) # <-- error captured: $ echo $msg -bash: log_file/doet/not/exist: No such file or directory
blog comments powered by Disqus