管道操作符和xargs的区别
之前一直没有理解shell中管道操作符和xargs的区别,为什么有一些命令要用可以直接用管道操作符链接,为什么一些需要加个xargs,最近翻了一些文档之后,终于理解
官方定义
最好的解释永远来自于官方文档,正所谓真传一句话,假传万卷书.
管道操作符
A pipeline is a sequence of one or more commands separated by one of the control operators ‘|’ or ‘|&’.
The output of each command in the pipeline is connected via a pipe to the input of the next command. That is, each command reads the previous command’s output. This connection is performed before any redirections specified by command1.
简单来说就是,管道操作符连接一系列命令,前面的标准输出作为后面的标准输入
xargs
The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings
as arguments.
Any arguments specified on the command line are given to utility upon each invocation, followed by some number of the arguments read from
the standard input of xargs. This is repeated until standard input is exhausted.
Spaces, tabs and newlines may be embedded in arguments using single ( ' '') or double (“‘’) quotes or backslashes (``'‘). Single
quotes escape all non-single quote characters, excluding newlines, up to the matching single quote. Double quotes escape all non-double
quote characters, excluding newlines, up to the matching double quote. Any single character, including newlines, may be escaped by a
backslash.
简单来说,就是xargs是从标准输入读取内容,作为xrags命令后面跟的命令的参数
举个例子
管道操作符
1 | echo "hello \n world" | grep hello |
echo "hello world" 会将hello world字符串输出到标准输出,然后通过管道操作符作为 grep hello这条命令的标准输入
上面的命令相当于下面的命令
1 | grep hello # 执行完按回车 |
xargs
1 | xargs ls #按回车 |
ctrl-d相当于向shell输入了一个eof,表示输入完成
组合
这两个命令一般是组合使用
比如:
1 | cat a.txt | xargs mkdir -p |
这条命令的含义是,将a.ext文件中的内容作为文件夹名,新建一个文件夹
其中,cat a.txt命令是将a.txt中的内容输出到标准输出,然后通过管道操作符作为xargs mkdir -p这个命令的标准输入,xargs将前面传过来的标准输入作为mkdir -p 这条命令的参数
标准输入/标准输出/参数
在linux中,默认情况下
标准输入是指键盘
标准输出是指屏幕
参数就是命令后面跟着的内容
举个例子
1 | cat hello-world.txt # hello-world.txt 是参数 |

