I used
exec > /dev/null
to suppress output.
Is there a command to undo this? (Without restarting the script.)
by Zeeman Memon. Whether you are a new Linux user or an experienced bash programmer, it is highly probable that you encountered the cryptic command 2>/dev/null. Although this command looks technically complex, its purpose is very simple. It refers to a null device that is used to suppress outputs of various commands.
In Unix, how do I redirect error messages to /dev/null? You can send output to /dev/null, by using command >/dev/null syntax. However, this will not work when command will use the standard error (FD # 2). So you need to modify >/dev/null as follows to redirect both output and errors to /dev/null.
/dev/null is a special filesystem object that discards everything written into it. Redirecting a stream into it means hiding your program's output. The 2>&1 part means "redirect the error stream into the output stream", so when you redirect the output stream, error stream gets redirected as well.
On Unix-like operating systems, exec is a builtin command of the Bash shell. It lets you execute a command that completely replaces the current process. The current shell process is destroyed, and entirely replaced by the command you specify.
To do it right, you need to copy the original FD 1 somewhere else before repointing it to /dev/null. In this case, I store a backup on FD 5:
exec 5>&1 >/dev/null
...
exec 1>&5
Another option is to redirect stdout within a block rather than using exec
:
{
...
} >/dev/null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With