417

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".

11 Answers 11

690

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
Sign up to request clarification or add additional context in comments.

6 Comments

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".
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, 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.
@yang /dev/null exists in Cygwin, you don't need to use nul.
What is "bit bucket"? Do you mean Bitbucket?
No. It's a 'turn of phrase' rather than a reference to a real thing. "Bit bucket" is a common term for /dev/null because it's a place where you dump a bunch of bits you don't care about.
61

Something like

script > /dev/null 2>&1

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

3 Comments

Also achieved with just &> if you want to save some typing ;)
Even though I'm adding this to my gs (GhostScript) command, it still prints out **** Warning: File has a corrupted %%EOF marker, or garbage after %%EOF. Any advice?
This does not run the process on unix systems , I had to do 2>/dev/null
21
+50

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

Comments

19

Try

: $(yourcommand)

: is short for "do nothing".

$() is just your command.

4 Comments

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
@olibre, what about DUMMY=$(yourcommand)? It do not have the limitation you have mentioned. :)
@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
An alternative is command | : which maybe easier to type
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

4 Comments

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: 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).
I don't think I've actually used NUL since DOS 5.x, had a sudden flash of remembrance when I saw this answer ;)
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, it worked like a charm.
6

This is another option

scriptname |& :

3 Comments

I think is probably the best one, considering it doesn't require buffering the output of the command in memory.
This is the more clever solution.
Why is this "clever"? : discards its input, so it works like /dev/null, but you are needlessly creating a pipe where none is necessary or useful.
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

Comments

4

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

foo 2>&- >&-

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

Comments

2

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 &)

3 Comments

can you explain what is the difference between the two?
@osbm: the difference is the 2>&1 part, which is redirection of stderr to stdout. The first command will only suppress stdout but not stderr; the 2nd will suppress both.
this made my day, the compiller was giving info. messages even when i routed > /dev/null this 2>&1 helped me removing redundant entries in output
2

I know this question is 15 years old, but it's the top result for searches for preventing output from a script, and there is (to my mind, at least) no valid answer given. The question specifically asks for something similar to the Windows "echo off" command. "echo off" is a command that is (or at least was, in the old cmd.com days) often put at the top of the script. It's a way to silence the script FROM WITHIN THE SCRIPT itself. All of the answer here seem to be addressing silencing the script from the command line when you run it, or silencing the output of specific lines within the script. I was looking for a way to silence all output from the script from within the script, and that's not found here.

The answer I found was to put this at the top of the script:

exec > /dev/null 2>&1

and at the bottom:

exec &>/dev/tty

Obviously, this prevents any errors from being seen and isn't appropriate for all use cases. If the script fails before restoring output in a session, you would see no output until you manually restored it.

Comments

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

4 Comments

never saw >> to /dev/null.. out of curiosity.. Why are you doing this?
Is it suppressing all output?
Why the double ">" (>>)?
Using >> to null is unnecessary and somewhat incorrect, but in this case, harmless.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.