AndrewLngdn
AndrewLngdn

Reputation: 73

grep tries to read from a file instead of through pipe when passing function arguments with a space

I'm trying to write a function that will find all processes by name and then let you send signals to them one by one. Here is the smallest part of code that I can't get to work correctly:

ps -ef | grep "$@"

If I used

ab cd

as the input to the function, I'd like it to generate

ps -ef | grep "ab cd"

but instead it generates

ps -ef | grep ab cd

which looks for 'ab' in the file 'cd'.

Upvotes: 0

Views: 41

Answers (1)

Amadan
Amadan

Reputation: 198294

You want "$*", not "$@". This answer goes into details, but basically "$@" quotes each parameter individually, while "$*" quotes the whole mess. So, "$@" is equivalent to "ab" "cd"; "$*" is equivalent to "ab cd".

Upvotes: 2

Related Questions