Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "< /dev/null >& /dev/null" at the end of a command do?

Tags:

linux

bash

stdio

One of the scripts I run over ssh was hanging and I found a solution for it on this site: http://www.snailbook.com/faq/background-jobs.auto.html

The site resolves the problem by adding this to the end of the command:

xterm < /dev/null >& /dev/null &

I think I know what part of it does, but can someone help explain?

The first part:

# For stdin, read from /dev/null
< /dev/null

The second part:

>& /dev/null

What does >& do? I've seen 2>&1 which is direct STDERR to STDOUT, but when there are no numbers, does that mean redirect everything to /dev/null?

like image 976
user215997 Avatar asked Nov 21 '11 06:11

user215997


People also ask

What is meant by dev null 2 >& 1?

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

Why do we use dev null in Linux?

Usage of /dev/null It is mainly used to discard standard output and standard error from an output.

What is dev null and dev Zero?

All write operations to /dev/zero succeed with no other effects. However, /dev/null is more commonly used for this purpose. When /dev/zero is memory-mapped, e.g., with mmap, to the virtual address space, it is equivalent to using anonymous memory; i.e. memory not connected to any file.

What is dev null in scripting?

Think of /dev/null as a black hole. It is essentially the equivalent of a write-only file. Everything written to it disappears. Attempts to read or output from it result in nothing. All the same, /dev/null can be quite useful from both the command-line and in scripts.


2 Answers

Googling for 'stream redirection >& ' will let you find http://tldp.org/LDP/abs/html/io-redirection.html

which, among other things, tells you

  >&j

  # Redirects, by default, file descriptor 1 (stdout) to j.
  # All stdout gets sent to file pointed to by j.

Maybe this already helps.

Or search the help of standard unix shells, like bash, for stream redirection.

Rgds,

Thomas

like image 180
Thomas Avatar answered Nov 15 '22 20:11

Thomas


Yes, this means redirect both stdout and stderr to /dev/null.

From info "(bash)Redirections":

Redirecting Standard Output and Standard Error

Bash allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of WORD with this construct.

There are two formats for redirecting standard output and standard error:

&>WORD

and

>&WORD

Of the two forms, the first is preferred. This is semantically equivalent to

>WORD 2>&1
like image 21
Michael Hoffman Avatar answered Nov 15 '22 20:11

Michael Hoffman