How to stop .bashrc from running sub-command in alias at startup?
Use pkill
instead:
alias killguake='pkill guake'
This is a whole lot safer than trying to parse the process table outputted by ps
.
Having said that, I will now explain why your alias doesn't do what you want, but really, use pkill
.
Your alias
alias killguake="kill -9 $(ps aux | grep guake | head -n -1 | awk '{print $2}')"
is double quoted. This means that when the shell parses that line in your shell initialization script, it will perform command substitutions ($( ... )
). So each time the file runs, instead of giving you an alias to kill guake
at a later time, it will give you an alias to kill the guake
process running right now.
If you list your aliases (with alias
), you'll see that this alias is something like
killguake='kill -9 91273'
or possibly even just
killquake='kill -9'
if guake
wasn't running at the time of shell startup.
To fix this (but really, just use pkill
) you need to use single quotes and escape the $
in the Awk script (which is now in double quotes):
alias killguake='kill -9 $(ps aux | grep guake | head -n -1 | awk "{print \$2}")'
One of the issues with this approach in general is that you will match the processes belonging to other users. Another is that you will possibly just find the grep guake
command instead of the intended process. Another is that it will throw an error if no process was found. Another is that you're invoking five external utilities to do the job of one.
Use pkill, don't reinvent the wheel!
alias killguake="pkill -9 guake"