You have stumbled upon one of the little-known shell-programming gotchas.
Normally, vi
would not be alias-expanded in that context, because alias
expansion only applies to the first word in a command (other than environment-variable assignments). However, as indicated by the bash manual:
If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.
Words cannot span an alias and its surrounding text, so there is no need for the space at the end of:
alias pa='ps ax | egrep -i '
If you change it to:
alias pa='ps ax | egrep -i'
things will work as expected. But you should really use shell functions instead of aliases; functions are more powerful, less quirky, and just as fast.
NB: For those searching for more evidence that bash is buggy, I hasten to add that the peculiar behaviour of aliases ending with blanks comes from the Posix specification, and will therefore be the same on any conformant shell:
If the value of the alias replacing the word ends in a <blank>
, the shell shall check the next command word for alias substitution; this process shall continue until a word is found that is not a valid alias or an alias value does not end in a <blank>
. (XSH, §2.3.1)