I have redircted my ouput using /dev/null in bash script but it is still throwing an error. Code is following

ps -p $proc | fgrep $proc> /dev/null
if [ $? -ne '0' ] ; then

below is error

error: list of process IDs must follow -p

 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).
Usage: fgrep [OPTION]... PATTERN [FILE]...
Try 'fgrep --help' for more information.

How can I suppress this error without affecting $? output?

  • 3
    Not really an answer to your question but I think you'd better check if $proc is empty before using it. – Lambert May 27 '15 at 6:26
  • 1
    @Aashu; What are you trying to do in the first place? - If you ps for a PID using -p the grep seems unnecessary. - You can do (for example): ps -p "$pid" >/dev/null 2>&1 || echo error, or: if ps -p "$pid" >/dev/null 2>&1 ; then echo okay ; else echo error ; fi. – Janis May 27 '15 at 6:40
  • Are you testing for existence of the PID $proc? If so then use test -n "$proc" && kill -0 "$proc" – roaima May 27 '15 at 7:12

You can use command grouping:

{ ps -p "$proc" | fgrep "$proc";} >/dev/null 2>&1

or wrap pipe in subshell:

(ps -p "$proc" | fgrep "$proc") >/dev/null 2>&1
  • You don't need a subshell if all you want is redirecting I/O; you can use command grouping: { ps -p "$proc" | fgrep "$proc" ;} >/dev/null 2>&1. – Janis May 27 '15 at 6:33
  • I never knew subshells exist. Now I know. – PNDA May 27 '15 at 6:34
  • @Janis: Good point, forgot about it. Updated my answer. Thanks. – cuonglm May 27 '15 at 6:36

&> /dev/null throws away both stderr and stdout. Same answer as the others, just a few characters shorter.


redirect error stream to null like ps -p $proc 2> /dev/null | fgrep $proc > /dev/null 2>&1

  • Still getting same error. – Aashu May 27 '15 at 6:24
  • edited the answer @Aashu, try it now – Bazi May 27 '15 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.