Self-Deleting Script for both Linux Bash and Windows Batch

George Hernando picture George Hernando · Aug 26, 2011 · Viewed 7.5k times · Source

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

Answer

michel-slm picture michel-slm · Aug 26, 2011

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`