Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a command-line shortcut for ">/dev/null 2>&1"

It's really annoying to type this whenever I don't want to see a program's output. I'd love to know if there is a shorter way to write:

$ program >/dev/null 2>&1

Generic shell is the best, but other shells would be interesting to know about too, especially bash or dash.


1 Answers

You can write a function for this:

function nullify() {
  "$@" >/dev/null 2>&1
}

To use this function:

nullify program arg1 arg2 ...

Of course, you can name the function whatever you want. It can be a single character for example.

By the way, you can use exec to redirect stdout and stderr to /dev/null temporarily. I don't know if this is helpful in your case, but I thought of sharing it.

# Save stdout, stderr to file descriptors 6, 7 respectively.
exec 6>&1 7>&2
# Redirect stdout, stderr to /dev/null
exec 1>/dev/null 2>/dev/null
# Run program.
program arg1 arg2 ...
# Restore stdout, stderr.
exec 1>&6 2>&7
like image 166
Ayman Hourieh Avatar answered Sep 11 '25 00:09

Ayman Hourieh