In the shell script, by default, there are always three files open: standard input (keyboard input), standard output (output to the screen) and standard error (also output to the screen). Their corresponding file descriptors are 0, 1 and 2 respectively.
&
Is a descriptor. If 1 or 2 is not preceded by &, it will be regarded as an ordinary file.>
The default is standard output redirection, and1>
identical1>&2
It means redirecting standard output to standard error2>&1
This means redirecting standard error output to standard output.&>filename
It means redirecting both standard output and standard error output to the file filename>&2
Namely1>&2
That is, output the result to the same as the standard error
/dev/null
It’s a file. This file is special. It discards all the things passed to it
Standard output and error output
> ls
rumenz.txt
> ls rumenz.txt 1.txt
ls: 1.txt: No such file or directory
rumenz.txt
- because
1.txt
Doesn’t exist, sols: 1.txt: No such file or directory
The error output is 2. rumenz.txt
The file exists, sorumenz.txt
Is standard output. It’s 1.
Redirect the above standard output and error output to the file
> ls rumenz.txt 1.txt 1>out.log 2>err.log
> cat out.log
rumenz.txt
> rumenz cat err.log
ls: 1.txt: No such file or directory
out.log
Stored is standard outputerr.log
Error output is stored
case analysis
> ls rumenz.txt 1.txt > out.txt
ls: 1.txt: No such file or directory
> cat out.txt
rumenz.txt
Because only standard output is redirected, so
out.txt
The file has only standard output
> ls rumenz.txt 1.txt > out.log 1>&2
ls: cannot access 1.txt: No such file or directory
rumenz.txt
> cat out.log
> out.log
Redirects standard output to a file, but1>&2
Redirected standard output to wrong output, soout.log
There’s nothing in it
> ls rumenz.txt 1.txt > out.txt 2>&1
> cat out.txt
ls: cannot access 1.txt: No such file or directory
rumenz.txt
> out.log
Redirects standard output to a file, but2>&1
Redirect error output to standard output, soout.log
There are both normal and wrong outputs
/dev/null
/dev/null
: represents a black hole, usually used to discard unwanted data output or empty files for input streams
> rm -f $(find / -name rumenz) &> /dev/null
Original link:https://rumenz.com/rumenbiji/…
WeChat official account: entry station