Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ampersand indicate in this bash command 1>&2

Tags:

bash

scripting

Quick one, 2>&1 redirects stderr to stdout, but what does the ampersand mean? I know if we had 2 > 1 it would output to a file named 1, what does the ampersand do?

like image 981
mikip Avatar asked Feb 26 '10 10:02

mikip


People also ask

What does the ampersand mean in bash?

An ampersand does the same thing as a semicolon or newline in that it indicates the end of a command, but it causes Bash to execute the command asynchronously. That means Bash will run it in the background and run the next command immediately after, without waiting for the former to end.

What is ampersand in Linux command?

Linux Ampersand (&) when a command line ends with the &, the shell does not wait for the command to finish. You will get your shell prompt back while the command executes in the background.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What is double ampersand in bash?

The command shell interprets the && as the logical AND. When using this command, the second command will be executed only when the first one has been succcefully executed.


2 Answers

2>&1 redirects standard error (file handle 2) to the same file that standard output (file handle 1) is currently going to.

It's also a position-dependent thing so:

prog >x 2>&1 >y 

will actually send standard error to x and standard output to y as follows:

  • Connect standard output to x;
  • Then connect standard error to same as current standard output, which is x;
  • Then connect standard output to y;
like image 79
paxdiablo Avatar answered Oct 17 '22 08:10

paxdiablo


The & in &1 duplicates the file descriptor 1. The duplicated descriptor actually does not behave like a copy, but like an alias of the old one. Duplicating 1 allows multiple streams to be redirected to 1 without overwriting each other.

Example: (no &)

$ ls existing-file non-existent-file > tmp 2> tmp  $ cat tmp existing-file nt-file: No such file or directory 

Note that 1 overwrote what 2 wrote. But not when we use &:

$ ls existing-file non-existent-file > tmp 2>&1  $ cat tmp ls: non-existent-file: No such file or directory existing-file 

A file descriptor is a handle to a file (or other input/output resource, such as a pipe or network socket). When 1 and 2 are separately redirected to tmp (as in the first example), they move their tmp file pointer independently. That's why the file descriptors overwrote each other.

According to the Linux man page:

[Duplicate file descriptors] refer to the same open file description and thus share file offset and file status flags; for example, if the file offset is modified by using lseek(2) on one of the descriptors, the offset is also changed for the other.

Note that even though & acts like an alias, 2>&1 means redirect 2 to the stream that 1 in currently pointing to. When 1 is redirected to something else, 2 points to the same file it did independently of 1.

Observe:

$ ls existing-file non-existent-file > tmp 2>&1 > tmp1 $ cat tmp1 existing-file $ cat tmp ls: non-existent-file: No such file or directory 
like image 26
Rose Perrone Avatar answered Oct 17 '22 06:10

Rose Perrone