/dev/null 2 &1 la gi

From the manual cron(8):

When executing commands, any output is mailed to the owner of the crontab […].

So what your article suggests here is to produce no output, thus sending no mail. Another way (more convenient?) to disable mail is to use the -m off option, i.e.

crond -m off

Now to the syntax: this is specific to the Bourne shell language (and its derivatives such as bash, zsh, and so on).

[n]>file [n]>fd

will redirect to file descriptor n (or standard output if unspecified) to file descriptor fd.

A file descriptor can be a file name of the address of a stream. & is the address operator as in the C language.

Conventionally, file descriptor 1 is standard output (a.k.a. stdout) and file descriptor 2 is standard error (a.k.a. stderr). The chunk

>/dev/null

is redirecting stdout to /dev/null.

'2>&1'

is redirecting the error stream to the output stream, which has been redirected to /dev/null. As such, no output is produced and no mail is sent.

Warning: the order of redirection matters:

>/dev/null 2>&1

is not the same as

2>&1 >/dev/null

Try these two commands with a non-privileged user:

ls >/dev/null 2>&1 ls 2>&1 >/dev/null

Indeed, in the later case, file descriptor 2 is set to the current address of file descriptor ``1 (which is stdout at this very moment), and then the file descriptor 1 is redirected to /dev/null. File descriptor 2 is still redirected to stdout, no matter what happens to file descriptor 1.

/dev/null 2 &1 la gi

If you’re quite new to the world of Linux many things will be new and become confusing, because Linux offers you so many variations of control which can be combined in several ways.

One thing of these is the control of the output from a command which runs in the terminal. It quite often happens that you want to prevent any output of a command e.g. in a cronjob or if run a command in background. This makes a lot of sense because if you’re running a command in background you simply don’t want that it throws all information and error to the terminal your’re actually working with.

At this point file descriptors come into place. Every Unix process has to support at least the three common descriptors defined by the POSIX application programming interface.

  • 0 Standard Input
  • 1 Standard Output
  • 2 Standard Error

In combination with the possibility to forward output with the “>” we can simply explain what “1>/dev/null 2>&1” actually means. If we start a command from the terminal e.g. G-edit this could look like this:

patrick@Linux: /usr/bin/gedit 1>/dev/null 2>&1

Lets explain every part of the command.

  • /usr/bin/gedit is the program we want to start
  • 1 is the file descriptor for Standard Output
  • > is for forwarding
  • /dev/null is a path to a black hole where any data sent, will be discarded (This could also be the path to a file)
  • 2 is the file descriptor for Standard Error
  • > is for forwarding
  • & is the symbol for file descriptor (without it, the following 1 would be considered as a filename)
  • 1 is the file descriptor for Standard Output

So in a sentence “1>/dev/null 2>&1” after a command means, that every Standard Error will be forwarded to the Standard Output and this will be also forwarded to a black hole where all information is lost.

Newcomers to Bash programming will sooner or later come across /dev/null and another obscure jargon: > /dev/null 2>&1. It may look confusing but it’s fairly simple to understand and a fundamental part of shell programming. So let’s break it down with step-by-step examples.

To begin, /dev/null is a special file called the null device in Unix systems. Colloquially it is also called the bit-bucket or the blackhole because it immediately discards anything written to it and only returns an end-of-file EOF when read.

Let’s see what happens when we try writing to it with the file redirection operator (>).

# First let's try writing to this file.
$ echo 'text' > /dev/null
# Upon inspection, we see that the write was successfully.
$ echo $?
0

The $? symbol is a special variable that always contains the exit status of the previous command; it will be overwritten every time you run a new command. By convention, an exit code of 0 indicates that the previous command was successful while anything greater indicates an error code for that specific program.

For example, if we lookup ls in the man pages, we’ll see that the exit code 1 corresponds to a minor problem.

/dev/null 2 &1 la gi

http://man7.org/linux/man-pages/man1/ls.1.html

Let’s look at another example. Given the following two commands: the first (ls) is a valid command while the second (ls -0) is an invalid command because it contains an illegal option -0.

This is a valid command:

$ ls
Applications Documents Library Music Public
$ echo $?
0

Now let’s look at the invalid command:

$ ls -0
ls: illegal option -- 0
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]
$ echo $?
1

The problem with the second script is that it displays any error messages into STDERR. However for our scripts, we want to suppress error messages. Luckily there’s a hack to do exactly what we want.

Let’s try that again with > /dev/null 2>&1:

$ ls -0 > /dev/null 2>&1
$ echo $?
1

Notice this time, that we didn’t see any error messages. To break this down, we’re suppressing the error output (stderr) of the ls -0 command, redirect it to standard output (stdout), writing it to /dev/null thereby immediately discarding it. The >& symbol is an operator that copies the output of the first file descriptor (2) and redirects to the output of the second file descriptor (1).

Now let’s see what the numbers in 2>&1 represent by looking at this chart of File Descriptors.

/dev/null 2 &1 la gi

We can verify this by outputting to a regular file instead of /dev/null.

$ ls -0 > /tmp/devnull 2>&1
$ echo $?
1
$ cat /tmp/devnull
ls: illegal option -- 0
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

This technique is commonly used to tell whether a command exists, which you can use for handling different operating systems, automatically installing packages, downloading files, and most importantly defending your scripts and systems from unexpected exceptions.

function cmd_exists() {
command -v $1 > /dev/null 2>&1
}
# cmd_exists ls; echo $?
# cmd_exists sl; echo $?

Hopefully /dev/null along with output redirection, exit statuses, file descriptors should make sense since they are fundamental to Bash programming. There are many other I/O operators each with their own specific purposes which you can view here:

What does Dev Null 2 mean?

After executing the ping command, '>/dev/null' tells the system to suppress the output, and '2>&1' directs the standard error stream to standard output. In this way, all output of the command is discarded.

What does this mean Dev null 2 >& 1?

So in a sentence “1>/dev/null 2>&1” after a command means, that every Standard Error will be forwarded to the Standard Output and this will be also forwarded to a black hole where all information is lost.

What does null 2 mean?

command >> /dev/null 2>&1 1 is standard output and 2 is standard error. 2>&1 redirects standard error to standard output. &1 indicates file descriptor (standard output), otherwise (if you use just 1 ) you will redirect standard error to a file named 1 .

What is the meaning of 2 >& 1 in Linux?

1 "Standard output" output file descriptor. The expression 2>&1 copies file descriptor 1 to location 2 , so any output written to 2 ("standard error") in the execution environment goes to the same file originally described by 1 ("standard output").