I sometimes would do

grep -ri foo .

but in some node_modules, it could have some links to places that don't exist. So I can do a

grep -ri foo . 2>&-


grep -ri foo . 2> /dev/null

but sometimes it is difficult to remember. Is it possible to write an alias so that

grep -ri foo . | discarderr 

can work? I think it might be difficult to do because only the stdout is passed to discarderr. There probably is no way to pass both stderr and stdout to discarderr and have discarderr discard the error, such as

alias discarderr='cat 2> /dev/null'

Is there any way to do it, or maybe by a function but it probably looks a bit awkward if it is something like discarderr(grep -ri foo .)?

It seems grep has a -s that can silence the error message. But what about a general solution for other commands?


A shell function that executes a command given as one or several arguments and discards its standard error stream:

discarderr () {
    "$@" 2>/dev/null

An alias that does the same:

alias discarderr='2>/dev/null'

Both would be used in the same way:

discarderr some-command

... although this is arguably obfuscating the command and more cumbersome than just adding 2>/dev/null to a command, in the long run. You would, for example, have to remember to instantiate the alias or function on each and every system you use, and if you use the function in a script, it hides the fact that you are discarding diagnostic messages from someone that skims the code.

Additionally, diagnostic messages are useful, and you should ideally correct things like dead symbolic links etc. rather than working around the fact that they causes errors in programs that tries to access data through them.

Also note that using discarderr would make it harder to debug "strange" issues. You can't, for example, see whether a command was executed at all as e.g. discarderr grap (grep misspelled) would not output anything.

| improve this answer | |
  • thank you. so "$@" means the whole command in a Bash function, and 2>/dev/null can be placed at the very front of a command – nonopolarity Feb 23 at 10:44
  • @nonopolarity "$@" means "all command line arguments, individually quoted" (in any POSIX shell), and yes, redirections can be placed anywhere on the command line, for example in the front of a command. – Kusalananda Feb 23 at 11:08
  • all the arguments... and argument #1 now becomes the command... I see. thanks. – nonopolarity Feb 23 at 12:19
  • "should ideally correct things like dead symbolic links etc"... the node_modules were created using create-react-app, which currently is npx create-react-app my-app... it was installing 300MB of node_modules and today it is 260MB... so I am not too sure about what happened in node_modules (which I don't touch directly) – nonopolarity Feb 23 at 12:23

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.