cmd > file: 輸出重定向到文件
cmd < file: 輸入重定向到文件
cmd >> file: 輸出追加重定向到文件
n > file: 將文件描述符爲n 的文件重定向到file
n >> file: 描述符爲n的文件追加重定向到file
n >$ m: 輸出文件m和n合併到m
n <$ m: 輸入文件m 合併 到n
<< tag: 開始標記tag和結束標記tag之間的內容作爲輸入
文件描述符:
STDIN: 0
STDOUT: 1
STDERR: 2
一般情況下,每個unix/linux命令運行時會打開3個文件:stdin, stdout, stderr.
cmd > file: stdout重定向到file,cmd < file: stdin重定向到file, cmd 2 > file: stderr 重定向到file,
cmd > file 2 >&1: stdout與stderr合併重定向到file,
cmd < file1 > file2: stdin重定向到file1, stdout重定向到file2
eg:
1. 輸出重定向
[root@k8s-master test3]# who > users
[root@k8s-master test3]# cat users
root tty1 2020-03-21 00:04
root pts/0 2020-03-21 00:05 (192.168.159.1)
[root@k8s-master test3]# who
root tty1 2020-03-21 00:04
root pts/0 2020-03-21 00:05 (192.168.159.1)
[root@k8s-master test3]#
2. 輸入重定向
[root@k8s-master test3]# wc -l ./t2.sh
5 ./t2.sh
[root@k8s-master test3]# wc -l < ./t2.sh
5
[root@k8s-master test3]#
[root@k8s-master test3]# wc -l ./t2.sh > wc-t2.txt
[root@k8s-master test3]# cat wc-t2.txt
5 ./t2.sh
[root@k8s-master test3]#
3. Here Document
cmd << delimiter
document
delimiter
兩個delimeter之間的內容重定向給stdin。
最後的delimiter必須頂格,前後不能有任何其他字符,空格也不可以。
[root@k8s-master test3]# wc -l <<EOF
> HELLO
> WORLD
> EOF
2
[root@k8s-master test3]#
4. 屏蔽輸出
/dev/null中寫入的內容全部會被丟棄,讀取不到任何內容。
cmd > /dev/null 2>&1