Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell - Suppress output of a single command

I have a rm *.o command in Makefile to remove all the object files generated during compilation. However some error information will be output if some .o file does not exist. So how can I suppress the error information output?

like image 513
CDT Avatar asked Mar 15 '13 12:03

CDT


2 Answers

In the context of make, more importantly than the output, you don't want make to treat the result of rm as failure. There are two ways to deal with it:

clean:
    -rm *.o    2> /dev/null
    rm -f *.o  2> /dev/null

The first way is to prefix the command with a minus sign, which tells make to ignore the return code. This is the preferred, make-specific way. The second is to use the -f flag, which is specific only to rm.

On top of that, you can choose to suppress the output with 2> /dev/null or not.

like image 63
Hai Vu Avatar answered Nov 07 '22 01:11

Hai Vu


In rare instances, it may also be necessary to redirect the output to /dev/null. You probably want to do rm *.o > /dev/null 2>&1. The > /dev/null part sends stdout to /dev/null and the 2>&1 says to send stderr to wherever stdout is going.

like image 6
Steve Avatar answered Nov 06 '22 23:11

Steve