If the directory containing your Lua interpreter is in your PATH environment variable, and you called the Lua interpreter by file name:
lua myprog.lua
then arg[-1] contains "lua", not the absolute path of the Lua interpreter.
The following Lua program works for me on z / OS UNIX:
-- Print the path of the Lua interpreter running this program posix = require("posix") stringx = require("pl.stringx") -- Returns output from system command, trimmed function system(cmd) local f = assert(io.popen(cmd, "r")) local s = assert(f:read("*a")) f:close() return stringx.strip(s) end -- Get process ID of current process -- (the Lua interpreter running this Lua program) local pid = posix.getpid("pid") -- Get the "command" (path) of the executable program for this process local path = system("ps -o comm= -p " .. pid) -- Is the path a symlink? local symlink = posix.readlink(path) if symlink then print("Path (a symlink): " .. path) print("Symlink refers to: " .. symlink) else print("Path (actual file, not a symlink): " .. path) end
Alternatively, on UNIX operating systems with the proc file system, you can use readlink("/proc/self/exe") to get the path.
source share