预备知识
多个命令称为过滤器(filter)。使用管道(pipe)连接每个过滤器。
cmd1 | cmd2 | cmd3
实战演练
- 组合两个命令
[root@shaofeng ~]# ls | cat -n > out.txt
[root@shaofeng ~]# cat out.txt
1 1.txt
2 backup
3 debug.sh
4 fname_test.sh
5 function.sh
6 IsRoot.sh
7 out.txt
8 printf.sh
9 script.sh
10 sleep.sh
11 time_take.sh
- 两种方式执行过滤器:$()或者``
[root@shaofeng ~]# ./ser.sh
1 1.txt 2 backup 3 debug.sh 4 fname_test.sh 5 function.sh 6 IsRoot.sh 7 out.txt 8 printf.sh 9 script.sh 10 ser.sh 11 sleep.sh 12 time_take.sh
1 1.txt 2 backup 3 debug.sh 4 fname_test.sh 5 function.sh 6 IsRoot.sh 7 out.txt 8 printf.sh 9 script.sh 10 ser.sh 11 sleep.sh 12 time_take.sh
[root@shaofeng ~]# cat ser.sh
#!/bin/bash
cmd_output=$(ls | cat -n)
echo $cmd_output
cmd_output=`ls | cat -n`
echo $cmd_output