该-w
标志的手段:
-w --ignore-all-space
Ignore all white space.
Run Code Online (Sandbox Code Playgroud)
现在,这不是很清楚,因为“空白”实际上\n
通常包括在内。例如,\s
Perl Compatible Regular Expressions 或 POSIX 中的类[[:space:]]
都匹配换行符:
$ printf '\n' | grep -zqP '\s' && echo yes
yes
$ printf '\n' | grep -zq '[[:space:]]' && echo yes
yes
Run Code Online (Sandbox Code Playgroud)
但是,diff
通过比较行和行是由\n
字符定义的(grep
实际上也是如此,这就是我必须在-z
上面使用的原因)。因此,diff
不能\n
考虑因为\n
意味着这是一条不同的线路。所以不,该-w
选项不会导致diff
丢弃换行符,空行表示文件不匹配:
$ printf 'foo\nbar\n' > file1
$ printf 'foo\n\nbar\n' > file2
$ diff -wq file1 file2
Files file1 and file2 differ
Run Code Online (Sandbox Code Playgroud)
但是,diff
由于行数,有一个选项确实可以忽略更改:
-B --ignore-blank-lines
Ignore changes whose lines are all blank.
$ diff -sB file1 file2
Files file1 and file2 are identical
Run Code Online (Sandbox Code Playgroud)