Bash redirection details

Source: Internet
Author: User

Bash redirection details first, let's review the existing bash redirection symbols 1. redirect input and output. The target is the file word [n]. <word defaults to 0 [n]> word defaults to 1 [n]> | word defaults to 1 noclobber, the example shows its usefulness. [n]> by default, word n is 1 igi @ igi-debian :~ $ Rm-f testfileigi @ igi-debian :~ $ Touch testfileigi @ igi-debian :~ $ Cat testfileigi @ igi-debian :~ $ Set-o noclobberigi @ igi-debian :~ $ Echo 2> testfilebash: testfile: cannot overwrite existing fileigi @ igi-debian :~ $ Echo 2> | testfileigi @ igi-debian :~ $ Cat testfile22. redirection standard errors and standard output to the specified file descriptor &> word more common> & word> word 2> & 1 append output &> word none> & word expression method> word 2> & 13. here Documents <[-] word here-documentdelimiter-deletes the tab character starting with all rows. 4. here Strings <word5. copy file descriptor [n] <& word: n is 0 by default. If it is a number, it must be an open file descriptor [n] <&-disable file descriptor [n]> & word. By default, n is 1. If it is a number, it must be an opened file descriptor [n]> &-disable file descriptor 6. move file descriptor [n] <& digit-default n is 0 [n]> & digit-default n is 17. open the file descriptor [n] In read/write mode <> when the word file is not present, it will be created. If you want to deeply understand the redirection, first understand the following 2 points 1. shell (Bash, csh, etc.) is responsible for redirection. For programs or functions, this is transparent. It only controls the input and output. As for the input and output, the shell interpreter is responsible for 2. in the shell Command Parsing process, I/O redirection is set before the command is actually executed. Let's take a look at the following example 1. 'echo 1 a1> a2 'and 'echo 1> a2 a1' igi @ igi-debian :~ $ Echo 1 a1> a2igi @ igi-debian :~ $ Cat a21 a1igi @ igi-debian :~ $ Rm a2igi @ igi-debian :~ $ Echo 1> a2 a1igi @ igi-debian :~ $ Cat a21 a1IO redirection is set before the command is executed, so in the above two cases, the final effect is the same. bash first redirects the output to the a2 file, run 'echo 1 a1' 2. 'ls nothisfile> res 2> & 1' and 'ls nothisfile 2> & 1> res 'igi @ igi-debian :~ /Rtest $ ls nothisfilels: cannot access nothisfile: No such file or directoryigi @ igi-debian :~ /Rtest $ ls nothisfile> res 2> & 1igi @ igi-debian :~ /Rtest $ cat resls: cannot access nothisfile: No such file or directoryigi @ igi-debian :~ /Rtest $ ls nothisfile 2> & 1> resls: cannot access nothisfile: No such file or directoryigi @ igi-debian :~ /Rtest $ cat resigi @ igi-debian :~ /Rtest $ ls-1abcresigi @ igi-debian :~ /Rtest $ ls-1 2> & 1> resigi @ igi-debian :~ /Rtest $ cat resabcres 'ls nothisfile> res 2> & 1', file descriptor 1 is redirected to the file res (originally standard output ), then, redirect file descriptor 2 to file descriptor 1 (in this case, file descriptor 1 points to file res), and finally execute "ls nothisfile" to generate an error and be sent to file descriptor 2, finally, the streaming file res. 'Ls nothisfile 2> & 1> res', file descriptor 2 is redirected to file descriptor 1 (standard output: screen), and then file descriptor 1 is redirected to file res, the result is that file descriptor 2 is redirected to the standard output, file descriptor 1 is redirected to file res, and the error generated by executing "ls nothisfile" is sent to the screen. 3. 'ls nothisfile a> & word' and 'ls nothisfile a> & 123456 'igi @ igi-debian :~ /Test/shell $ ls-1aigi @ igi-debian :~ /Test/shell $ cat athis is aigi @ igi-debian :~ /Test/shell $ ls nothisfile a> & wordigi @ igi-debian :~ /Test/shell $ cat wordls: cannot access nothisfile: No such file or directoryaigi @ igi-debian :~ /Test/shell $ ls nothisfile a> & amp; 123456-bash: 123456: Bad file descriptorigi @ igi-debian :~ /Test/shell $ cat 123456cat: 123456: No such file or directory> & this redirection symbol, as mentioned earlier, "redirection standard error and standard output to specified file descriptor" and "copy file descriptor" both have this symbol, in fact, "redirecting standard errors and standard output to a specified file descriptor" is a special situation of "copying file descriptors, that is, when [n]> & word n is omitted and word is not a number, the standard error and standard output will be redirected to the specified file. "Ls nothisfile a> & word". Since word is not a pure number, bash parses it into "redirection standard error and standard output to specified file descriptor". the effect is equivalent to "ls nothisfile a> word 2> & 1" "ls nothisfile a> & 123456". Since 123456 is a pure number, bash resolves it to "copy file descriptor ", it is equivalent to "ls nothisfile a 1> & 123456", but an error occurs because the "copy file descriptor" specifies that "if it is a number, it must be an opened file descriptor. 4. 'ls a 1> &-'and 'ls a> & 1-'igi @ igi-debian :~ /Test/shell $ lsaigi @ igi-debian :~ /Test/shell $ cat athis is aigi @ igi-debian :~ /Test/shell $ ls a> & 1-aigi @ igi-debian :~ /Test/shell $ ls a 1> &-ls: write error: Bad file descriptor "ls a> & 1-",> & 1-"[n]> & digit-" mentioned in "Mobile file descriptor", replace file descriptor n with the file descriptor digit, and n is disabled. n is 1 by default. "ls a> & 1-", equivalent to "ls a 1> & 1-". Replace file descriptor 1 with the original file descriptor 1, and then disable the original file descriptor 1, the output is still sent to the screen. "ls a 1> &-",> &-the output belongs to the "Close file descriptor" mentioned in "copy file descriptor ", "ls a 1> &-", disable file descriptor 1 and run "ls a". Because the output is sent to file descriptor 1 by default, it is disabled, "error file descriptor" 5. 'ls a nothisfile 1> & 2-'and 'ls a nothisfile 1 <& 2-' I Gi @ igi-debian :~ /Test/shell $ ls-1aigi @ igi-debian :~ /Test/shell $ ls a nothisfile 1 <& 2-aigi @ igi-debian :~ /Test/shell $ ls a nothisfile 1> & 2-aigi @ igi-debian :~ /Test/shell $ exec 3 <> testigi @ igi-debian :~ /Test/shell $ ls a nothisfile 1> & 3-ls: cannot access nothisfile: No such file or directoryigi @ igi-debian :~ /Test/shell $ cat testaigi @ igi-debian :~ /Test/shell $'1> & 2-'and '1 <& 2-' are effective, [n]> & digit-and [n] <& digit-the operations on the two mobile file descriptors are all from digit to n. The difference is that n is not specified, <& digit-equal to 0 <& digit-, and> & digit-equal to 1> & digit-move the file descriptor to point the descriptor digit to n, then digit is disabled. in this example, exec is a good explanation of the behavior of moving file descriptors. file descriptor 3 is directed to the file test, and then 1> & 3 -, in this way, 1 will also be directed to the 3 targeted file test, and the file profile 3 will be closed. The effect is that the standard output will be directed to the test file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.