Search code examples
luanullenvironment-variables

why does a simple print(os.getenv("HOME")) get nil?


I'm a total newbie and I don't get why this doesn't work, anyone knows what's happening?.

I'm following a tutorial and just to clarify, it is only a print(os.getenv("HOME"))


Solution

  • How to solve this:

    print(os.getenv("HOME")) prints nil. So os.getenv("HOME") must return nil.

    In which cases does os.getenv return nil? Let's refer to the Lua manual:

    https://www.lua.org/manual/5.4/manual.html#pdf-os.getenv

    os.getenv (varname)

    Returns the value of the process environment variable varname or fail if the variable is not defined.

    fail equals nil. Lua < 5.4 returns nil.

    So looks like HOME is an undefined process environment variable.

    Should it be defined? What is HOME to begin with?

    Let's google "environment variable home" and among the first few hits we see:

    https://superuser.com/questions/607105/is-the-home-environment-variable-normally-set-in-windows

    We are running Windows, otherwise we wouldn't be here in the first place so let's run print(os.getenv("UserProfile")) and we have success.