即使存在 -B(--ignore-blank-lines)选项,为什么 linux shell diff 命令有时会显示与空行相关的输出?

问题描述 投票:0回答:0

Linux diff 命令接受一个选项,指示它根据手册页“忽略行全为空白的更改”。

在尝试这个选项时,我遇到了一个空行似乎没有被忽略的情况,即使使用了 -B(或 --ignore-blank-lines)选项。

以下是演示该问题的工作脚本:

#!/bin/bash

echo -e 'a\nb\nc' > file_a1
echo -e 'a\n\nb'  > file_a2

echo
echo \ file_a1
cat    file_a1
# a
# b
# c

echo
echo \ file_a2
cat    file_a2
# a
#
# b

echo
echo \ regular diff
diff    file_a1 file_a2
# 1a2
# >        <- output related to empty line
# 3d3
# < c

echo
echo \ diff -B : empty lines ignored, as expected
diff -B file_a1 file_a2
# 3d3
# < c

echo -e 'a\nb' > file_b1
echo -e '\nb'  > file_b2

echo
echo \ file_b1
cat    file_b1
# a
# b

echo
echo \ file_b2
cat    file_b2
#
# b

echo
echo \ regular diff
diff    file_b1 file_b2
# 1c1
# < a
# ---
# >        <- output related to empty line

echo
echo \ diff -B : same output, as if -B option was not present
diff -B file_b1 file_b2
# 1c1
# < a
# ---
# >        <- output related to empty line

echo
echo \ diff version
diff --version
# diff (GNU diffutils) 3.9
# Copyright (C) 2023 Free Software Foundation, Inc.
# License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
# 
# Written by Paul Eggert, Mike Haertel, David Hayes,
# Richard Stallman, and Len Tower.
linux bash shell diff
© www.soinside.com 2019 - 2024. All rights reserved.