Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to execute a program which loops indefinitely, and so must be forcibly interrupted. After executing the program, I want to delete it. I can do it in several steps:

  1. ./MyProg
  2. Control + C
  3. rm MyProg

but I need to do this often, so I'd like to have steps 1 and 3 on a single line for easy history recall.

I have tried the following, but in each case the Control + C interrupt kills the whole line, not just the executing program, and so the file is never deleted:

./MyProg && rm MyProg
./MyProg;   rm MyProg
./MyProg || rm MyProg

How can I get the interrupt to affect only the running program, and not the whole command line?

share|improve this question

You can use the trap mechanism inside your MyProg to clean up after the program is exiting.

trap "rm MyProg" EXIT

If the MyProg is not a bash script, you can wrap your program in a bash script.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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