bash – Why Doesn’t My Command Work When Aliased

aliasbashquotingshell

I use ps -ef | grep catalina | grep -v grep to print the tomcat process running on the system:

kshitiz   7099     1  0 May11 ?        00:02:29 /usr/lib/jvm/jdk1.8.0/bin/java -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:38156 -Dcatalina.base=/home/kshitiz/Documents/workspaces/ggts/.metadata/.plugins/org.eclipse.wst.server.core/tmp1 -Dcatalina.home=/opt/tomcat-7.0.42 -Dwtp.deploy=/home/kshitiz/Documents/workspaces/ggts/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps -Djava.endorsed.dirs=/opt/tomcat-7.0.42/endorsed -Dfile.encoding=UTF-8 -classpath /opt/tomcat-7.0.42/bin/bootstrap.jar:/opt/tomcat-7.0.42/bin/tomcat-juli.jar:/usr/lib/jvm/jdk1.8.0/lib/tools.jar org.apache.catalina.startup.Bootstrap start

Then I use ps -ef | grep catalina | grep -v grep | awk -F' ' '{print $2}' to extract the process id:

7099

But when I alias the whole command alias tomcat_id="ps -ef | grep catalina | grep -v grep | awk -F' ' '{print $2}'" and use it via alias, it prints the whole text and awk doesn't seem to work.

type tomcat_id gives:

tomcat_id is aliased to `ps -ef | grep catalina | grep -v grep | awk -F' ' '{print }''

Best Answer

A general rule of aliases: if it gets too complex for an alias, use a function instead.

tomcat_id () {
  ps -ef | grep catalina | grep -v grep | awk -F' ' '{print $2}'
}

The problem with your alias definition is that you got the quoting wrong. The line that defines the alias is a shell command and is parsed by the shell. Since you used double quotes, and there are probably no positional parameters at the time (so $2 expands to an empty string), the alias you've defined is

ps -ef | grep catalina | grep -v grep | awk -F' ' '{print }'

The easy way to define an alias is to use single quotes for the definition, and avoid using single quotes in the aliased command. While I'm at it, I've removed -F " ", which is the default anyway (and a bit weird because it looks like “a space” but means “any sequence of whitespace”).

alias tomcat_id='ps -ef | grep catalina | grep -v grep | awk "{print \$2}"'

Or you can use '\'' to effectively quote a single quote inside a single-quoted literal.

alias tomcat_id='ps -ef | grep catalina | grep -v grep | awk '\''{print \$2}'\'

But, as I wrote above, use a function, it's clearer. Or rather, use the appropriate tool for the job:

alias tomcat_id='pgrep catalina'

or

alias tomcat_id='pidof catalina'
Related Question