File descriptorBefore a process reads a file, it needs to open the file. When the process opens the file, Linux associates the file with a number (called a file descriptor ), the file descriptor is an index of the opened file in the input table. Each process has its own set of opened files and their respective file descriptors. Once a file is opened, the process can read and write the file through the file descriptor. When a process does not need a file, it must close the file and release the file descriptor.
When a Linux process starts, three files are opened: standard input (file descriptor 0), standard output (file descriptor 1), and standard error (file descriptor 2 ). The child process inherits the file descriptor of the parent process. You can run the LS/proc/self/FD command to view the currently opened file descriptor.
Exec n> OUTFILE indicates opening an input file OUTFILE and associating it with the file descriptor N; Exec m <infile indicates opening an input file infile, and associate it with the file descriptor m;
<&> &<&> & Indicates that the file descriptor N is opened or redirected using exec n <& M, and is used as a copy of input file M.
Use exec n> & M to open or redirect the file descriptor N and use it as a copy of the output file M.
One instance:
Exec 4 <& 0 # Use 4 to save the copy of the standard input exec <$1 # redirect the standard input to file 1 exec 7> & 1 # use 7 to save the standard output exec> $2 # redirect standard output to file 2 cat-| tr a-Z A-Z # convert standard input to large and small output to standard output
Exec 1> & 7 7> &-# use 7 to restore standard output, disable 7 exec 0 <& 4 4 <&-# Use 4 to restore standard input, and disable 4
A shell redirection note