<p>How can i, in a bash script, execute a command when the user stops the script (with ctrl - c)?</p>

<p>Currently, i have this:</p>

<pre><code>afplay file.mp3

while true:
do osascript -e "set volume 10"
end
</code></pre>

<p>But i would like it to execute <code>killall afplay</code> when the user is finished with it, regardless if it is command-c or another keypress.</p>

```
trap 'killall afplay' EXIT
```