Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know that having an & at the end of a command makes it run in the background. I was wondering what does &Number mean? I was looking at a script that has some commands running with &1 and some with &2.

share|improve this question
    
I'd change the question to What does an &Number in the shell do? Ampersand is used all over in Linux, the question is slightly ambiguous until you read details –  Rich Homolka Sep 3 '10 at 20:31
add comment

migrated from stackoverflow.com Sep 3 '10 at 20:03

This question came from our site for professional and enthusiast programmers.

4 Answers

up vote 5 down vote accepted

This should be migrated to SuperUser, but the answer is that they refer to file descriptors. &1 is standard output, and &2 is standard error, so a program run like this:

./program 2>&1

Redirects descriptor 2 (standard error) to descriptor 1 (standard output)

A common use of this is to do something like this:

./program >file.txt 2>&1

Which redirects standard output into a file, and then standard error into standard output, so that you get both regular and error output captured in the file.

share|improve this answer
1  
see gnu.org/software/bash/manual/bashref.html section "3.6 Redirections" for more detail –  David Sep 3 '10 at 20:03
add comment

&1 is standard output (STD_OUT), &2 is standard error stream (STD_ERR)

share|improve this answer
add comment

For Bourne type shells (bash etc.)

mycmd > /output/file 2>&1

merges the standard error output (file descriptor 2) with the standard output (file descriptor 1) and writes both of them into /output/file.

share|improve this answer
add comment

The '&' indicates a file descriptor. &1 is stdout, and &2 is stderr.

If you haven't found a good site for learning shell scripting already, I recommend this one:

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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