I am wondering if there is a way of getting the path to the currently executing lua script file?
This is specifically not the current working directory, which could be entirely different. I know luafilesystem will let me get the current working directory, but it doesn't seem to be able to tell the current executing script file.
Thanks
EDIT: I'm not running from the standard command line interpreter, I am executing the scripts from a C++ binary via luabind.
The standard ELT development environment provides some lua files to set default variables ( PATH , LD_LIBRARY_PATH …). There are located under: /elt/System/modulefiles.
Software developers typically open and modify LUA files with source code editors, such as Microsoft Visual Studio Code and MacroMates TextMate. Plain text editors, which include Microsoft Notepad and Apple TextEdit, may also be used to open and modify LUA files.
The dofile function is actually an auxiliary function; loadfile does the hard work. Like dofile , loadfile also loads a Lua chunk from a file, but it does not run the chunk. Instead, it only compiles the chunk and returns the compiled chunk as a function.
This is a more elegant way:
function script_path() local str = debug.getinfo(2, "S").source:sub(2) return str:match("(.*/)") end print(script_path())
If the Lua script is being run by the standard command line interpreter, then try arg[0]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With