Search code examples
windowsbashbatch-fileself-destruction

Self-Deleting Script for both Linux Bash and Windows Batch


I have an uninstall script that cleans up an add-on tool used with an application. Versions of the script run on both Windows and Linux.

I'd like to be able to delete the uninstall script file and also the directory in which the script runs too (in both the case of a Windows batch file and also for the case of a Linux bash file). Right now everything but the script and the directory in which it runs remains after it runs.

How can I delete the script and the script's directory?

Thanks


Solution

  • In Bash, you can do

    #!/bin/bash
    # do your uninstallation here
    # ...
    # and now remove the script
    rm $0
    # and the entire directory
    rmdir `dirname $0`