0

How can we redirect output of command to /dev/null?

I tried it with:

command > /dev/null

But it didn't work.

cuonglm
  • 153,898
Laki
  • 555
  • 1
  • 4
  • 11
  • Have you tried command 2>&1 > /dev/null? – YoMismo Oct 08 '15 at 09:57
  • that work for me, what are you mean by "don't work"? Maybe you see the STRERROR, not the STROUTPUT. –  Oct 08 '15 at 10:00
  • that syntax I used won't work. @gio900 – Laki Oct 08 '15 at 10:01
  • what command your are trying for command > /dev/null? what it outputs? – Pandya Oct 08 '15 at 10:19
  • /usr/bin/amixer cset numid=28 118 – Laki Oct 08 '15 at 10:21
  • @cuonglm tell that to the OP, He said it DID solve his problem. Reread the comments. – YoMismo Oct 08 '15 at 10:33
  • @YoMismo: No, it can be the command he ran success. Try ls file-does-not-exist 2>&1 >/dev/null. – cuonglm Oct 08 '15 at 10:38
  • 1
    @YoMismo Since the OP has not explained what command he is using and whether it is printing to stdout or stderr, cuonglm is quite right in pointing out that what you show can fail. Even if it works for this particular case, it might not for the next person to read your comment and it's good to mention this. On the other hand, your comment is way out of line. If that's how you're going to behave towards others, then please do stay away. Rudeness is not acceptable. You're welcome to participate and I hope you do, but only if you can be civil. – terdon Oct 08 '15 at 11:53
  • @Terdon there is a difference between can fail and wrong. (Someone erased that comment). Discrediting another one is not the way to go, correcting or improving is. As I stated in my previous (erased) comment no one (AFAIK) gets paid for answering and trying to help, you just do it in your spare time because you want to help. When someone (a few people that can be counted with one hand's fingers) doesn't like an aswer or comment because it is not PERFECT just down votes or discredits the comment, so don't worry about me you won't see any answer (good or bad) from me any more. – YoMismo Oct 08 '15 at 13:22
  • @YoMismo command 2>&1 > /dev/null doesn't work because the redirection is read from right to left so the >/dev/null is applied before the 2>&1. So yes, it is actually wrong. However, even if it weren't, that wouldn't give you the right to use the kind of language you did. I'll be sorry to see you go, you post good content, all you need to do is be civil. – terdon Oct 08 '15 at 13:32

1 Answers1

2

Your command had redirected standard output to /dev/null, but maybe the command printed to standard error instead of standard output, so you still see the text in terminal.

To redirect standard output and standard error to /dev/null, you need:

command >/dev/null 2>&1

Note that the order of redirection is important, swapping them to 2>&1 >/dev/null won't work because standard error was redirected to standard output at the time when standard output still point to terminal, you still see the text in terminal if command write to standard error.

cuonglm
  • 153,898