bash alias help

I try the following alias:
alias psx='ps auxw | grep $1 | grep -v grep'
but it fails with "grep: yyy: No such file or directory" (for 'psx yyy' command).

the following is ok:
alias psx='ps auxw | grep $1'

why this happens?
how to overcome this?

thanks

An alias cannot have an argument like $1. You want to use a function instead.

psx () {
  ps auxw | grep "$1" | grep -v grep
}

Your second alias works by coincidence when $1 is empty in the current shell (as it will often be in an interactive bash); it expands to ps auxw | grep (empty string) yyy -- and ironically, if you had double-quoted $1 properly, it would also not have worked.

Your first alias expands to ps auxw | grep (empty string) | grep -v grep yyy

Search these forums for ways to avoid the grep -v grep elegantly; solutions have been posted a number of times.

You might it helpful to investigate the pgrep command if it is available on your system (I think it is on Solaris and Linux).

thanks!

Apparently many make this mistake, I found more than once $1's for aliases in .bashrc files...