What is </dev/null in bash?

bash

Solution

`</dev/null` is used to avoid having the script wait for input.

Quoting from the usage of < /dev/null & in the command line:

`< /dev/null` is used to instantly send EOF to the program, so that it doesn't wait for input (`/dev/null`, the null device, is a special file that discards all data written to it, but reports that the write operation succeeded, and provides no data to any process that reads from it, yielding EOF immediately). `&` is a special type of command separator used to background the preceding process.

So the command:

nohup myscript.sh >myscript.log 2>&1 </dev/null &
#\__/             \___________/ \__/ \________/ ^
# |                    |          |      |      |
# |                    |          |      |  run in background
# |                    |          |      |
# |                    |          |   don't expect input
# |                    |          |   
# |                    |        redirect stderr to stdout
# |                    |           
# |                    redirect stdout to myscript.log
# |
# keep the command running 
# no matter whether the connection is lost or you logout

will move to background the command, outputting both stdout and stderr to `myscript.log` without waiting for any input.

Problem

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)

Original source

Related problems