Reputation: 6132
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".
Upvotes: 408
Views: 495287
Reputation: 131
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.
Upvotes: 2
Reputation: 33605
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 &)
Upvotes: 2
Reputation: 457
>&-
closes the filedescriptor without even redirecting to /dev/null
foo 2>&- >&-
Can close stdin too
foo 2>&- >&- <&-
Upvotes: 4
Reputation: 41670
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
Upvotes: 8
Reputation: 13762
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
Upvotes: 680
Reputation: 101
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
Upvotes: 1
Reputation: 7523
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
Upvotes: 21
Reputation: 10065
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
Upvotes: 4
Reputation: 191
Try
: $(yourcommand)
:
is short for "do nothing".
$()
is just your command.
Upvotes: 19
Reputation: 29021
Something like
script > /dev/null 2>&1
This will prevent standard output and error output, redirecting them both to /dev/null
.
Upvotes: 60