diff 可以比较两个文件,那么能否比较两个字符串呢(主要是懒得创建文件),其实是可以的,如:
不仅仅diff可以这么玩, vimdiff也能这么玩,如:
1 |
#vimdiff <(echo 0) <(echo 1) |
那么,比较的两部分算是什么呢?
其实也是两个文件,如下:
这是两个什么文件呢?其实是两个pipe,如下:
这里vimdiff还为这两个pipe创建了两个交换文件.
知识点:
bash中 “<(list)” 是一种语法结构,叫做Process Substitution :
3.5.6 Process Substitution
Process substitution allows a process’s input or output to be referred to using a filename. It takes the form of
1 |
<(list) |
or
1 |
>(list) |
The process list is run asynchronously, and its input or output appears as a filename. This filename is passed as an argument to the current command as the result of the expansion. If the >(list)
form is used, writing to the file will provide input for list. If the <(list)
form is used, the file passed as an argument should be read to obtain the output of list. Note that no space may appear between the <
or >
and the left parenthesis, otherwise the construct would be interpreted as a redirection. Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files.
When available, process substitution is performed simultaneously with parameter and variable expansion, command substitution, and arithmetic expansion.
该语法结构在zsh中记作: =(list)
注意1:
这里产生的文件是管道,是管道,是管道,只能读一次,不能到做普通文件使用,如:
这里cat了两次,只输出一次hello
为什么写个简单的测试还要放在function里面?拿出来不好使
脚本:
1 2 3 4 5 6 7 |
#!/bin/bash function hello() { f=<(echo hello) cat $f cat $f } hello |
注意2:
进程替换只对于bash生效,对于sh不生效; sh 不理解进程替换,至少对于4.2.46版本的bash来讲,如果把bash mv成为 sh,就不再理解进程替换了; 更有甚者,如果 sh 软连接到bash,然后使用sh执行上面脚本就得出现错误
参考:
https://unix.stackexchange.com/questions/62140/filesize-difference-of-same-name-folders
http://tiswww.case.edu/php/chet/bash/bashref.html#Process-Substitution