1

I've been using the following to launch programs from terminals:

program_name >/dev/null 2>&1 &

But recently I came across this webpage where the following method was recommended:

program_name </dev/null &>/dev/null &

Now, I know what the first one means. It means to point stderr to stdout and stdout to /dev/null i.e. both stderr and stdout is now pointed to null.

But what does the second one means? And which one is more suitable for launching programs headlessly from terminal?

4

There are, by default, three "standard" files open when you run a program, standard input (stdin), standard output (stdout), and standard error (stderr). In Unix, those are associated with "file descriptors" (stdin = 0, stdout = 1, stderr = 2). By default, all three are associated with the device that controls your terminal (the interface through which you see content on screen and type input into the program).

The shell gives you the ability to "redirect" file descriptors. The > operator redirects output; the < redirects input.

For example:

program_name > /dev/null

Which is equivalent to:

program_name 1> /dev/null

Redirects the output to file descriptor 1 ('stdout') to `/dev/null'

Similarlly,

program_name 2> /dev/null

Redirects the output to file descriptor 2 ('stderr') to '/dev/null'

You might want to redirect both stdout and stderr to a single file, so you might think you'd do:

program_name > /dev/null 2> /dev/null

But that doesn't handle interleaving writes to the file descriptors (the details behind this are a different question). To address this, you can do:

program_name > /dev/null 2>&1

Which says "redirect writes to file descriptor 1 to /dev/null and redirect writes to file descriptor 2 to the same place as the writes to file descriptor 1 are going". This handles interleaving writes to the file descriptors.

That option is so common that some shells include a short-hand that is shorter and functionally equivalent:

program_name &> /dev/null

Finally, you can redirect input in the same way that you redirect output:

program_name < /dev/null

Will redirect file descriptor 0 (stdin) from /dev/null, so if the program tries to read input, it'll get EOF.

Putting that all together:

program_name </dev/null &>/dev/null &

Say (1) run program_name, (2) redirect standard input from /dev/null (</dev/null), (3) redirect both file descriptors 1 and 2 (stdout and stderr) to /dev/null (&>/dev/null), and (4) run the program in the background (&).

  • Why does & in &>/dev/null means fd 1 and fd 2? Is that the short-hand you talked about above this ending? – klaus Jan 28 at 14:54
  • The last & is to run the program in the background -- it's unrelated to redirection. > redirects standard output; 2> redirects standard error. – Andy Dalton Jan 28 at 15:12
  • is program_name </dev/null >/dev/null 2>&1 & equivalent to program_name </dev/null &>/dev/null &? – klaus Jan 28 at 15:18
  • 1
    Yes, like I said in my answer above, foo &> bar is a short-hand for foo > bar 2&1. The < /dev/null means the same in both instances. – Andy Dalton Jan 28 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.