Note: this trick also applies to scripting languages other than Lua, or to any other document type that might be considered an application in some contexts and a document in others.
If an interpreter is already associated to handle .lua files in Windows (i.e. if double clicking on a .lua file runs it in the interpreter) it is easy to make them executable directly on the command line.
You need to:
- put the script in a directory that is listed in the
PATHenvironment variable; - add the
.luaextension to thePATHEXTenvironment variable.
In this way you can run any Lua script by typing its name on the command line. The problem now is that you cannot put another Lua script in any directory on the PATH without risking it to be executed, even if you don't want it to be executed. (An example of such a .lua file would be a module private to your program that will be loaded by require. Modules are by convention also named .lua, but rarely support execution as a free-standing program.)
So it is an all-or-nothing situation. Either you allow any script in a directory on the path to be executed or none.
Is there a way to allow only some scripts in a directory on the path to be runnable typing their name?
scriptingumbrella. Since my use case was essentially that of Lua and the examples provided were about Lua I decided to use Lua tag for fear that without it the community could flag the post as off-topic or not well documented (my initial note reflects this mixed feeling). - Lorenzo Donati -- Codidact.com