Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is </dev/null in bash?

Tags:

bash

In bash, standard (1) and error (2) output can be re-routed and discarded with:

>/dev/null 2>&1 

But the following example does something different:

nohup myscript.sh >myscript.log 2>&1 </dev/null & 

What is the meaning/function of </dev/null in the example above? In what sort of scripting scenario would it be useful?

(Example Source)

like image 364
nmax Avatar asked Nov 13 '13 13:11

nmax


People also ask

What is Dev null used for?

/dev/null is a null device–a special type of virtual device. It is present in every Linux system, and the purpose of this device is to discard anything sent to it and read the End of File (EOF). Most virtual devices are used to read data; however, /dev/null is unique since it is used to suppress any data written to it.

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 tail Dev null?

tail -f /dev/null is usually added because the process (pid 1) in your docker container is not running in the foreground and if nothing is running in the foreground, docker automatically closes itself.

What does Dev null do in Unix?

The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection. The /dev/null device is a special file, not a directory, so one cannot move a whole file or directory into it with the Unix mv command.

What is/dev/null in Linux?

/dev/null is a null device–a special type of virtual device. It is present in every Linux system, and the purpose of this device is to discard anything sent to it and read the End of File (EOF). Most virtual devices are used to read data; however, /dev/null is unique since it is used to suppress any data written to it.

What is the Bash null command?

The Bash null command : is a shell builtin defined in the POSIX standard. It is there mostly for historical reason and compatibility with older shells but it can still serve a purpose in Bash today.

Why does/Dev/Null Output go to the file on the right?

You're using /dev/null differently. cat /dev/null outputs the "contents" of /dev/null, which is to say its output is blank. > messages (or > wtmp) causes this blank output to be redirected to the file on the right side of the > operator.

Why do we redirect to/dev/null in Linux?

Another reason to redirect to /dev/null is to prevent an unused file descriptor being created for stdin. This can minimize the total open file handles when you have many long running processes. Show activity on this post. </dev/null is used to avoid having the script wait for input.


Video Answer


1 Answers

Redirecting /dev/null to stdin will give an immediate EOF to any read call from that process. This is typically useful to detach a process from a tty (such a process is called a daemon). For example, when starting a background process remotely over ssh, you must redirect stdin to prevent the process waiting for local input.

Another reason to redirect to /dev/null is to prevent an unused file descriptor being created for stdin. This can minimize the total open file handles when you have many long running processes.

like image 89
cmh Avatar answered Nov 04 '22 14:11

cmh