views:

780

answers:

4

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.

+2  A: 
>& /dev/null
andrew
I'm really curious to know what you're running that forces you to pipe stdout and stderr to /dev/null so frequently that typing /dev/null becomes an annoyance.
Laurence Gonsalves
ephemient
A: 

If /dev/null is too much to type, you could (as root) do something like:

ln -s /dev/null /n

Then you could just do:

program >/n 2>&1

But of course, scripts you write in this way won't be portable to other systems without setting up that symlink first.

Greg Hewgill
+2  A: 

Most shells support aliases. For instance, in my .zshrc I have things like:

alias -g no='2> /dev/null > /dev/null'

Then I just type

program no
swampsjohn
In bash, that would be: alias no='2> /dev/null > /dev/null'; no command arg arg
lhunath
+3  A: 

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
Ayman Hourieh