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
Using a function instead of an alias avoids most of these quoting problems:
If you're using awk, don't need grep.
Or, stick with a function and avoid almost all the work you're doing:
You can "glue" single quotes with double quotes :
Here is an interesting reference : https://stackoverflow.com/questions/1250079/escaping-single-quotes-within-single-quoted-strings
However, there are simpler solutions to kill a process instead of using multiple pipes or additional single quotes (Cf others answers). Here i was just trying to answer your initial question, keeping your logic.
Here are the essentials for alias quoting:
There is very little that cannot be done, virtually anything that can be typed on the bash command line can be put into an alias.
Instead of running these multiple pipes, use arguments to
ps
to get only the pid to start with: