如何在pgrep中搜索不区分大小写?

tim*_*nen 13 bash

pgrep使用正则表达式作为模式样式.我想念grep的-i选项,告诉pgrep,我正在搜索不区分大小写.

另一种选择是

ps ax | grep -i PATTERN
Run Code Online (Sandbox Code Playgroud)

但是我必须使用PID发送KILL信号.使用pgrep和pkill组合,我可以使用相同的模式来杀死应用程序.

如何在bash上动态使用正则表达式的REG_ICASE?

Dim*_*lov 9

如果字符串不是太长:

pkill -f '[Pp][Aa][Tt][Ee][Rr][Nn]'
Run Code Online (Sandbox Code Playgroud)


j03*_*03m 7

kill `ps ax | grep -i PATTERN | awk '{ print $1 }'`
Run Code Online (Sandbox Code Playgroud)

将使用神奇的`杀死所有进程匹配不区分大小写的进程

  • 如果我可能这样建议,这里不需要grep,awk可以完成case-insensetive模式匹配的工作`ps ax | awk'BEGIN {IGNORECASE = 1}/PATTERN/{print $ 1}'` (2认同)

Rak*_*iha 5

[仅限 BSD]
[ FreeBSD pgrep 手册页]

来自pgrep 的手册页man pgrep在选项下i

 -i          Ignore case distinctions in both the process table and the
             supplied pattern.
Run Code Online (Sandbox Code Playgroud)

因此,我们可以使用如下选项i

pgrep -fi 'PATTERN'
Run Code Online (Sandbox Code Playgroud)

IE:

pgrep -f 'chrome'
echo $?
1
Run Code Online (Sandbox Code Playgroud)

但包括-fi选项有效:

pgrep -fi 'ChRoMe'
Run Code Online (Sandbox Code Playgroud)

输出:

>   872
    910
    41391
    60087
    60090
    60092
Run Code Online (Sandbox Code Playgroud)

  • 难怪@BSD。GNU 版本中不存在该选项。BSD 在答案中值得注意 (3认同)