7

I would like to print a list of all environment variables and their values. I searched the Stackoverflow and the following questions come close but don't answer me:

Unlike C, Lua doesn't have envp** parameter that's passed to main() so I couldn't find a way to get a list of all environment variables. Does anybody know how I can get the list of the name and value of all environment variables?

5 Answers 5

5

Standard Lua functions are based on C-standard functions, and there is no C-standard function to get all the environment variables. Therefore, there is no Lua standard function to do it either.

You will have to use a module like luaex, which provides this functionality.

1
  • 4
    @AlexStack: Not quite. argv refers to the command-line arguments passed to the program. envp refers to environment variables, but it's non-standard. Mar 4, 2013 at 18:39
2

This code was extracted from an old POSIX binding.

static int Pgetenv(lua_State *L)        /** getenv([name]) */
{
    if (lua_isnone(L, 1))
    {
        extern char **environ;
        char **e;
        if (*environ==NULL) lua_pushnil(L); else lua_newtable(L);
        for (e=environ; *e!=NULL; e++)
        {
            char *s=*e;
            char *eq=strchr(s, '=');
            if (eq==NULL)       /* will this ever happen? */
            {
                lua_pushstring(L,s);
                lua_pushboolean(L,0);
            }
            else
            {
                lua_pushlstring(L,s,eq-s);
                lua_pushstring(L,eq+1);
            }
            lua_settable(L,-3);
        }
    }
    else
        lua_pushstring(L, getenv(luaL_checkstring(L, 1)));
    return 1;
}
2
  • well, this is one way to solve the problem using C. Apparently there's no pure-Lua function that does this. Thanks anyway.
    – AlexStack
    Oct 7, 2011 at 14:24
  • 2
    @AlexStack, as mentioned by Nicol, there is no pure-Lua function because environ is not ANSI C, just POSIX.
    – lhf
    Oct 7, 2011 at 14:48
2

You can install the lua-posix module. Alternatively, RedHat installations have POSIX routines built-in, but to enable them, you have to do a trick:

  cd /usr/lib64/lua/5.1/
  # (replace 5.1 with your version)
  ln -s ../../librpmio.so.1 posix.so
  # (replace the "1" as needed)
  lua -lposix
  > for i, s in pairs(posix.getenv()) do print(i,s,"\n") end

The trick is in creating a soft-link to the RPM's "io" directory and to naming the soft-link the same name of the library LUA will attempt to open. If you don't do this, you get:

./librpmio.so: undefined symbol: luaopen_librpmio

or similar.

2
local osEnv = {}

for line in io.popen("set"):lines() do 
  envName = line:match("^[^=]+")
  osEnv[envName] = os.getenv(envName)
end

this would not work in some cases, like "no valid shell for the user running your app"

1
  • 1
    It could also fail if some environment variable names or values contain newlines or other funny characters. Oct 14, 2015 at 22:47
1

An easy 2 liner:

    buf = io.popen("env", '*r')
    output = buf:read('*a')
    print(output) -- or do whatever

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.