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)"
}
Best Answer
It's not impossible at all.
or,
The thing with aliases is that quoting may be tricky to get right as they are text strings, and that they are better suited for really short things, like
In almost every other instance, you want a shell function instead:
Or, corrected to not use the
update.sh
name in the current directory (it may be taken by an unrelated file) and to only runwget
if thecd
succeeded, and slightly streamlined for thebash
shell,(Note that you may want to run the
update.sh
script usingbash
instead of sourcing it, or pipe it tobash -s
, or use whatever interpreter its#!
-line uses. The fact that you're usingsource
on it is confusing as you're also running it in a subshell. The effect on the environment of running the script withsource
, which is usually why one wants tosource
a script, is lost when the subshell terminates.)Shell functions may be define in the same initialization file that you define aliases in, and they are used in the same way as aliases, but are more versatile (can take arguments etc.)
The
bash
manual contains the statement