Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo exec > /dev/null in bash?

Tags:

bash

I used

exec > /dev/null

to suppress output.

Is there a command to undo this? (Without restarting the script.)

like image 865
user2267134 Avatar asked Jul 24 '13 16:07

user2267134


People also ask

What is 2 >/ dev null in bash?

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.

How do I redirect error to Dev Null?

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.

What does >/ dev null 2 >& 1 mean?

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

What is exec $@ in bash?

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.


1 Answers

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
like image 149
Charles Duffy Avatar answered Oct 02 '22 12:10

Charles Duffy