“command not found” message is redirected too

2019-08-14 05:33发布

问题:

$ foobar
sh: foobar: not found
$ foobar 2>/dev/null
$ 

I'm trying to redirect only foobar's stderr (if foobar exists and can be run), but the shell's error message get redirected too. This happens in bash, ksh and sh, but not in csh. I'm not familiar with bourne shell source code, but I guess this happens because the shell first forks, then redirects, then tries exec(), and when exec() fails it sends an error message to stderr that has already been redirected.

回答1:

If foobar is some executable then try:-

$ ./foobar
sh: foobar: not found
$ ./foobar 2>/dev/null #alternatively you can use full path.
$ 


标签: bash shell