"command not found" message is redirected too
Asked Answered
M

1

1
$ 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.

Mickey answered 8/10, 2012 at 16:19 Comment(1)
when you actually have a foobar that produces messages to stderr, the 2>/dev/null will capture it. Use shell error codes to determine if cmd was found. Usually it is 127 to indicate cmd-not-found. Good question about fork/redirect/exec, but don't know the answer. Questions here on S.O. from students writing their own shell occasionally have answers that make the order or fork/exec/redirect explicit. Good luck.Wilber
P
0

If foobar is some executable then try:-

$ ./foobar
sh: foobar: not found
$ ./foobar 2>/dev/null #alternatively you can use full path.
$ 
Preindicate answered 8/10, 2012 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.