I want to ask a question about redirecting standard output and error to the same file using a Bash Shell.
I am a beginner in the Linux Command Prompt and am reading a book titled "The Linux Command Line" by William E. Shotts. Jr.
In a section on redirection, he states that to redirect standard output and error to the same file, the following command is written:
ls -l /bin/usr > ls-output.txt 2>&1
From what I understand, the /bin/usr
directory does not exist, so an error is thrown up and is sent to the standard error file. The output of the command ls -l /bin/usr
is redirected to a text file ls-output.txt
and the standard error is redirected to standard output with 2>&1
.
I'm confused here on two aspects:
Firstly, the order of events. By my intuition, since I thought commands are executed in an L-R fashion (Left-to-Right), it appears that the redirection of the
ls
command takes place first towardsls-output.txt
before the redirection of the standard error stream (2
) to the standard output stream (1
).The use of the
ampersand
. I understand from a Google search that if&
is placed at the end of a command, it means that other commands can be input before the initial command is executed - which has application when the initial command is coupled with a timer. I believe that the ampersand here implies that the&
means that we are redirecting to a file descriptor and not a file name named1
, although I'm unsure whether this is correct.
Overall, I have two questions:
- What is the order of execution of the command above?
- Am I correct in the function of the ampersand
&
? If not, what function does it serve?