Why the alias doesn't work
alias wd='ps -ef | grep java | awk {'print $2 " " $9'} | egrep "(A|B|C|D)"'
The alias
command receives three arguments. The first is the string wd=ps -ef | grep java | awk {print
(the single quotes prevent the characters between them from having a special meaning). The second argument consists of a single space character. (In .bashrc
, the positional parameters $2
and $9
are empty, so $2
expands to a list of 0 words.) The third argument is } | egrep "(A|B|C|D)"
(again the single quotes protect the special characters).
The alias definition is parsed like any other shell command when it is encountered. Then the string defined for the alias is parsed when the alias is expanded. Here are some possible ways to define this alias. First possibility: since the whole alias definition is within single quotes, only use double quotes in the commands, which means you must protect the "
and $
meant for awk with backslashes.
alias wd='ps -ef | grep java | awk "{print \$2 \" \" \$9}" | egrep "(A|B|C|D)"'
Second possibility: every character stands for itself within single quotes, except that a single quote ends the literal string. '\''
is an idiom for “single quote inside a single-quoted string”: end the single-quoted string, put a literal single quote, and immediately start a new single-quoted string. Since there's no intervening space, it's still the same word.
alias wd='ps -ef | grep java | awk '\''{print $2 " " $9}'\'' | egrep "(A|B|C|D)"'
You can simplify this a bit:
alias wd='ps -ef | grep java | awk '\''{print $2, $9}'\'' | egrep "(A|B|C|D)"'
Tip: use set -x
to see how the shell is expanding your commands.
Why the function doesn't work
I don't know. The part you show looks ok. If you still don't understand why your function isn't working after my explanations, copy-paste your code.
Alias or function?
Use an alias only for very simple things, typically to give a shorter name to a frequently-used command or provide default options. Examples:
alias grep='grep --color'
alias cp='cp -i'
alias j=jobs
For anything more complicated, use functions.
What you should have written
Instead of parsing the ps
output, make it generate output that suits you.
wd () {
ps -C java -o pid=,cmd= | egrep "(A|B|C|D)"
}
As @AndreasWiese comments, you can't do this with an alias. Aliases tack on arguments space-separated, so you can't join them (in a simple way) to the aliased command.
A comment on your function: Why are you using $@
? You should use $1
instead. If you call your function with multiple arguments (p a b c
), you'll probably get an error (b: command not found
) an unknown directory.
Best Answer
What you're asking doesn't really make much sense.
keyword means it's a word that is part of the syntax of the shell. Those are recognised through tokenising. Quoting them is enough for the shell to stop recognising.
It is possible to alias a keyword in most shells though. So, aliases have precedence over keywords (in effect they are expanded early, and there is more tokenising after that where you can have more aliases, keywords)...
aliases (with the exception of zsh aliases defined with
alias -g
are only expanded in command position), so typically not inbuiltin the-alias
.functions have precedence over builtins, and builtins over external commands (and then
$PATH
decides which one to use).You can force the builtin with:
(though it should be noted it is not a standard command).
You can disable aliases by quoting them (though the quoted version could also be aliased in some shells).
So, for instance (here zsh syntax), in:
Writing
while true
would actually outputalias while true
, and there would be no way to use thewhile
keyword since quoting it would disable both the alias and keyword.You would call the
while
function with for instance:If there was a
while
builtin (but of course their wouldn't in those shells that havewhile
keyword), you'd write it:And to call the
while
command, you'd write:or in
zsh
(when not insh
emulation):(in other shells,
command
only prevents functions, not builtins)Or
Of course, nothing's stopping you from doing even sillier things like:
Which, at least in zsh is valid (but stupid).