Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.