Make Scripts Executable Automatically

Bozhidar Batsov has a splendid tip over at Emacs Redux. I’m pretty sure he’s mentioned it before but it’s so useful it bears repeating. The tip is how to make scripts executable. “Scripts” means a file that has a shebang line. That is, a line that starts with #! followed by an application to execute.

The usual workflow is to write the script, save the file, and then change its permissions to make it executable. But it turns out that Emacs provides a better—or at least easier—way. The trick is to use the executable-make-buffer-file-executable-if-script-p command. It checks for the shebang line, makes sure that no execute bits are already set, and if not sets the execute bits respecting the umask.

The usual procedure is to call executable-make-buffer-file-executable-if-script-p from a hook function as described by Batsov in his post. If you’ve every written a script and tried to run it only to discover that it’s not executable, this tip will save you some time and frustration.

This entry was posted in General and tagged . Bookmark the permalink.