0

I'm trying to set an export that would effectively kill a process based on the PID that pgrep feeds it. Running the command kill $(pgrep myAppName) appears to work (if 'myAppName' were the name of an actual running process)... But how would you save this command as an exported environment variable? Every variation of the command that I could think of seems to try and evaluate pgrep myAppName into something like 20137 (or a similar PID value), and running the exported variable works the first time, but obviously won't work after that since the exported command is actually kill 20137, where that process has already been terminated.

How can I get this to be properly saved in a variable, so I can just type something like $KILL_APPNAME to kill that process, every single time the command is run?

  • 1
    Use pkill myAppName... – jasonwryan Mar 21 '15 at 8:05
  • Storing a command in a shell variable (whether exported or not — it wouldn't really need to be exported) sounds like the wrong solution to problem you're not mentioning. You could use seom trickery with eval but a shell function or even an alias sounds like a better way to do this. – Celada Mar 21 '15 at 8:07
  • Or killall. I think the direct answer to your question is "use a function", though. – Michael Homer Mar 21 '15 at 8:07
  • @Celada True, I'm asking this question to solve 2 problems... But I'm not shaving a yak, if that's what you're trying to say. I'm simply trying to learn how to solve one problem on a broader scale, while using another issue as a use case. Two birds, one stone. – RectangleEquals Mar 21 '15 at 8:19
  • @jasonwryan Well that solves the smaller issue, but I'd still like an answer to the actual question... And no, I don't wish to write a shell script in order to execute 1 line of code. Haven't looked into alias yet, but i'll check it out – RectangleEquals Mar 21 '15 at 8:27
2

What I think you're trying to do needs eval in order to work:

THING="eval kill \$(pgrep myAppName)"
$THING

but it's awkward and doesn't work by default in all shells (for example it doesn't work by default in zsh although an option can be set to make it work). Whatever you are trying to do is most likely better solved with something cleaner and easier like a function:

kill_my_app() {
    kill $(pgrep myAppName)
}
kill_my_app

Or, as others have pointed out in comments, just use pkill or killall which does exactly what you need.

  • Thank you, I've learned a lot here! But what's the escape sequence for (the ` before the $`)? What does it do in this scenario? – RectangleEquals Mar 21 '15 at 9:02
  • 1
    To be clear it's not a ` (backtick), it's a \​ (backslash). I think you had the same trouble I had getting it to show up correctly in the markup syntax before I edited this comment to fix it. It prevents the $ from being interpreted while you are setting the THING variable. The $ only gets interpreted later, when eval is executed. Try set -x to enable debugging in your shell to see what it's doing. Turn debugging off again with set +x. – Celada Mar 21 '15 at 9:22
  • Sorry, that was a typo. Seems to have something to do with stack exchange's metadata printing it out as a backtick (as you can see, you've also done here)... Might need to double-escape it to print out correctly in a code wrapper. But once again, thanks for the knowledge! – RectangleEquals Mar 21 '15 at 9:27
  • Yeah, geez, the markup syntax really gave us trouble here. I had to edit my comment, like, 5 times to get it to look right and even then I used a really dirty trick to force the backslash to display! :-) Anyway, you're welcome. – Celada Mar 21 '15 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.