Linux – how to make alias with quotes

aliaslinuxshell

I tried to make alias with quotes as following:

alias myalias='ps -ef | grep tomcat | kill -9 `awk {'print $2'}`'

but as you can see i already have ' in awk

so i tried to replace

awk {'print $2'}

with

awk {"print $2"}

but then strange things happen to me when i run this alias, ie, the console window get closed…
how can i make this alias work

Best Answer

Using a function instead of an alias avoids most of these quoting problems:

myfn() { ps -ef | awk '/tomcat/ {print $2}' | xargs kill -9; }

If you're using awk, don't need grep.

Or, stick with a function and avoid almost all the work you're doing:

alias myalias='pkill -9 -f tomcat'
Related Topic