382

I have a Bash script that runs a program with parameters. That program outputs some status (doing this, doing that...). There isn't any option for this program to be quiet. How can I prevent the script from displaying anything?

I am looking for something like Windows' "echo off".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
6bytes
  • 5,858
  • 9
  • 37
  • 42

10 Answers10

652

The following sends standard output to the null device (bit bucket).

scriptname >/dev/null

And if you also want error messages to be sent there, use one of (the first may not work in all shells):

scriptname &>/dev/null
scriptname >/dev/null 2>&1
scriptname >/dev/null 2>/dev/null

And, if you want to record the messages, but not see them, replace /dev/null with an actual file, such as:

scriptname &>scriptname.out

For completeness, under Windows cmd.exe (where "nul" is the equivalent of "/dev/null"), it is:

scriptname >nul 2>nul
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
andynormancx
  • 13,421
  • 6
  • 36
  • 52
  • 14
    Note that '&>' is peculiar to bash (and maybe C shells); Korn and Bourne shells require 2> /dev/null or 2>&1 (to send stderr to the same place as stdout). The Korn shell seems to interpret '&>' as "run the stuff up to & in background, then do the i/o redirection on an empty command". – Jonathan Leffler Mar 06 '09 at 00:16
  • 5
    Note that some commands write directly to the terminal device rather than to standard output/error. To test, put `echo foo > $(tty)` in a script and run `./test.sh &> /dev/null` - The output is still printed to the terminal. Of course this is not a problem if you're writing a script which uses no such commands. – l0b0 Feb 13 '13 at 12:19
  • 2
    @l0b0, is there a way to make the script's (or any other program's) tty different so that all output is redirected regardless of your example? I know screen and script can do something like that but both are finicky and vary from *nix to *nix. – Brent Nov 25 '14 at 20:50
  • @yang `/dev/null` exists in Cygwin, you don't need to use `nul`. – dimo414 Jan 31 '16 at 20:54
57

Something like

script > /dev/null 2>&1

This will prevent standard output and error output, redirecting them both to /dev/null.

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
20

An alternative that may fit in some situations is to assign the result of a command to a variable:

$ DUMMY=$( grep root /etc/passwd 2>&1 )
$ echo $?
0
$ DUMMY=$( grep r00t /etc/passwd 2>&1 )
$ echo $?
1

Since Bash and other POSIX commandline interpreters does not consider variable assignments as a command, the present command's return code is respected.

Note: assignement with the typeset or declare keyword is considered as a command, so the evaluated return code in case is the assignement itself and not the command executed in the sub-shell:

$ declare DUMMY=$( grep r00t /etc/passwd 2>&1 )
$ echo $?
0
Sylvain Bugat
  • 7,704
  • 3
  • 29
  • 30
semente
  • 7,265
  • 3
  • 34
  • 35
17

Try

: $(yourcommand)

: is short for "do nothing".

$() is just your command.

Pang
  • 9,564
  • 146
  • 81
  • 122
V0idSt4r
  • 171
  • 1
  • 2
  • 4
    Very interesting but there is a limitation: the returned status code is always `0` (success). Let's see an example using command `false` that always returns `1` (failure). For instance `( echo foo | tee file ; false ) && echo bar` displays `foo`. Using your trick to suppress the output: `( : $(echo foo | tee file ; false) ) && echo bar`. But it displays `bar` meaning the return status code is `0` (this is not the returned status code from `false`). Please update your answer providing this limitation. Cheers – oHo Nov 05 '15 at 12:40
  • 3
    @olibre, what about `DUMMY=$(yourcommand)`? It do not have the limitation you have mentioned. :) – semente Mar 14 '16 at 02:14
  • 3
    @semente Perfect: The command `DUMMY=$( echo foo | tee file ; false ) && echo bar` does not display anything. And the command `DUMMY=$( echo foo | tee file ; true ) && echo bar` displays `bar`. Let me know if your provide an answer to upvote it ;-) Cheers – oHo Mar 16 '16 at 21:50
  • An alternative is `command | :` which maybe easier to type – nadapez Feb 11 '22 at 15:01
8

Like andynormancx' post, use this (if you're working in an Unix environment):

scriptname > /dev/null

Or you can use this (if you're working in a Windows environment):

scriptname > nul
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83
  • 1
    You can actually do better than that in the Windows shell. If instead you use "scriptname > nul" then you won't even have a file to delete, as "nul" is the Windows equivalent of /dev/null. – andynormancx Mar 05 '09 at 23:50
  • 1
    @andynormancx: That's what I hate about the Windows file system layout. It "reserves" filenames like NUL, COM1, LPT1, CON, etc, no matter what directory you're in (even if you're in a file-system that can have those filenames, like in network shares). – dreamlax Mar 06 '09 at 02:16
  • I don't think I've actually used NUL since DOS 5.x, had a sudden flash of remembrance when I saw this answer ;) – andynormancx Mar 09 '09 at 13:46
  • In case you used `> nul` on Windows with cygwin or git bash and now you are stuck with a `nul` file that can't be deleted, please do give a try to [this answer](https://stackoverflow.com/questions/17883481/delete-a-file-named-nul-on-windows), it worked like a charm. – Daemon Painter Feb 04 '20 at 09:04
4

This is another option

scriptname |& :
Zombo
  • 1
  • 62
  • 391
  • 407
4

Take a look at this example from The Linux Documentation Project:

3.6 Sample: stderr and stdout 2 file

This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.

     rm -f $(find / -name core) &> /dev/null 

That said, you can use this simple redirection:

/path/to/command &>/dev/null
ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
1

In your script you can add the following to the lines that you know are going to give an output:

some_code 2>>/dev/null

Or else you can also try

some_code >>/dev/null
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rohan Dsouza
  • 81
  • 1
  • 2
  • 5
0

>&- closes the filedescriptor without even redirecting to /dev/null

foo 2>&- >&-

Can close stdin too foo 2>&- >&- <&-

Brian White
  • 389
  • 4
  • 9
0

If you're running a command in the background and want to suppress its output, the syntax is the following:

(my_command > /dev/null &)
or
(my_command > /dev/null 2>&1 &)

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335