2

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?

3

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.

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.