Creatively trying to prevent bash script execution
Posted on 2026-10-28
While working on a project, I was wondering if I could make it very difficult to execute a script. I'm creating bash functions in separate files, and they're replacing scripts that used to be executed directly. I am removing execute permissions, but some well-meaning future self might try and "fix" them by re-adding those permissions. Could I stop them from being executed in other ways?
I had a hunch, so I slightly edited the shebang line. You don't need to specify a real shell; I've seen awk scripts that specify it should be run with the awk binary, so why not throw in a curve ball?
#!/usr/bin/false
echo 'This aught to be source-able, but will fail if it is executed'
Here are the results of running this various ways, from an existing bash prompt: mixed success at best!
./script.sh- no output, return code 1bash script.sh(with and without-fflag) - output printed successfully, return code 0source script.sh- output printed successfully, return code 0. script.sh- output printed successfully, return code 0command script.sh(with script in the PATH) - no output, return code 1
Not terribly useful, but curious? Definitely.