Search code examples
python-3.xbashenvironment-variablessublimetext3manjaro

How to hide passwords and secret keys in environment variables ? (Linux)


I am using manjaro and bash shell. I am trying to hide passwords and secret keys in environment variables so that I can use them in my python script.

I tried this in my .bashrc file

export BOT_EMAIL="mymail@automail.com"
export BOT_PASS="pass_"

And if i run this script from terminal it runs

import os

a = os.environ.get("BOT_EMAIL")
b = os.environ.get("BOT_PASS")

print(a, b)

and gives me this output as expected

mymail@automail.com pass_

But the problem arrives when i try to run the script from sublime text 3 it gives me this

None None

which i think is sublime problem so I restarted sublime about ten times and even restarted my computer but it keeps giving None.

I am noob in programming and linux it would be very helpful if someone can solve this problem.


Solution

  • Sublime inherits its environment variables from the environment it's started in. So, if you start it from bash, it will be able to read all the variables you had set when it started. If you start it from the sublime_text.desktop file (an icon in a menu or on the side bar), it'll only be able to read the env variables that were set when the Launcher process started up -- i.e., at login. So, if you don't want to always start from the command line, put your environment variables in your ~/.profile file, then log out and back in. Putting them in ~/.bashrc won't work, as that's only read for interactive sessions.

    Alternatively, you can define env vars in your build system. A sample one might look something like this:

    {
        "shell_cmd": "python3 -u \"$file\"",
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python",
    
        "env": {"PYTHONIOENCODING": "utf-8",
                "MYVAR1": "value1",
                "MYVAR2": "value2"
        }
    }
    

    This has the advantage of being able to change variables and values without having to log out and back in.