iggy
iggy

Reputation: 1743

Bash trap exit kill last process

I have the following in my bash file: (I would like to kill a web server once the bash script is over under any circumstances)

python -m SimpleHTTPServer 12345 &
trap "kill $!" EXIT

I am wondering how safe/widespread is this? When will $! actually be evaluated (I am pretty sure that this happens at the place of declaration, but still need advice)?

Upvotes: 0

Views: 94

Answers (1)

John Kugelman
John Kugelman

Reputation: 361605

What you wrote is safe. Because you're using double quotes, $! is evaluated immediately. If you used single quotes it would be evaluated at the time the script exits.

Upvotes: 5

Related Questions