I'm writing a bash script where I want to grep the name of a process. This name is the argument when the script is executed.
Normally I can do this ps -ef | grep [w]ord to get the correct processes.
How can I have the same effect when the argument to grep (here [w]ord) is something like $1
example:
ps -ef | grep [f]fmpeg
will show lines like this and nothing else
jan       117977  117966  0 20:07 pts/0    00:00:01 ffmpeg ..
when I want the same behavior within a script and make the argument to grep the argument from the script:
ps -ef | grep "$1"
and call it with ./script.sh ffmpeg it will show me the ffmpeg processes (correct) but also the grep and bash process:
jan       117977  117966  0 20:07 pts/0    00:00:01 ffmpeg ..
jan       118566  117021  0 20:14 pts/4    00:00:00 bash ./script.sh ffmpeg
jan       118568  118566  0 20:14 pts/4    00:00:00 grep ffmpeg
I'm not interested in the last two lines of the output



pswith-fdisplays the arg lists of the processes (same asps -o args), not the process name (ps -o comm). To select processes by name (or arg list), usepgrep(orpgrep -f).ps -ef | grep [w]ordis wrong.[w]ordis also a glob to the shell, so is expanded to matching files first. Had you used a saner shell likezsh, you'd have seen ano matcherror (assuming there's no file calledwordin the current working directory).ps -Af | grep '[w]ord'