在bash中,在Mac上的终端中(但不在Linux中),grep -v -f的行为会有所不同,具体取决于它是在命令行还是在脚本中执行。从命令行:
$ touch empty-file #create an empty file
$ printf 'foo' | grep -v -f empty-file
foo
这是预期的。但是当该行在脚本中时,它什么都不输出。这是脚本:
$ cat grep-v-in-script.sh
#!/usr/bin/env bash
printf 'foo\n' | grep -v -f empty-file
printf 'end of script\n'
当我执行该脚本时:
$ ./grep-v-in-script.sh
end of script
如果我在Linux中运行相同的脚本,它按预期工作:
herdrick@some-linux-server:~$ ./grep-v-in-script.sh
foo
end of script
如果我将'grep -v -f'更改为'grep -f',我的Mac上的FWIW会再次输出,但这次是预期的。
这是我的bash版本:
$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Copyright (C) 2007 Free Software Foundation, Inc.
问题只是GNU grep和BSD grep之间不兼容。查看@ that-other-guy对帖子的评论。
我的困惑是由于我的别名设置为使用GNU grep。否则,在命令行和脚本中执行此操作没有区别。