Get ZSH environment variable in Ruby - ruby-on-rails

I've set an environment variable in my ~/.zshrc file and verified that is shows in the terminal with printenv:
AWS_ACCOUNT_ID=111111111
AWS_ACCESS_KEY_ID=222222222
AWS_SECRET_ACCESS_KEY=AAAAABBBBBBBBB34B3B3B3B3B3B3B3B3B
STRIPE_PUBLISHABLE_KEY=pk_test_1111111111111111
STRIPE_SECRET_KEY=sk_test_222222222222222
I've also checked that I can get the value in irb:
ENV["STRIPE_SECRET_KEY"]
=> "sk_test_222222222222222"
However, I'm getting nothing for the value in my Ruby script. It doesn't return anything. All I'm running at this point is:
key = ENV["STRIPE_SECRET_KEY"]
puts key
Which returns a blank space (not nil) or anything.
What am I missing?

When you run your command with sudo, it's possible the user running the script is not your user account (but depending on the system it could be root).
Therefore, you need to make sure that the user running the script is actually the one you setup the environment variable for.

Related

activating conda env vs calling python interpreter from conda env

What exactly is the difference between these two operations?
source activate python3_env && python my_script.py
and
~/anaconda3/envs/python3_env/bin/python my_script.py ?
It appears that activating the environment adds some variables to $PATH, but the second method seems to access all the modules installed in python3_env. Is there anything else going on under the hood?
You are correct, activating the environment adds some directories to the PATH environment variable. In particular, this will allow any binaries or scripts installed in the environment to be run first, instead of the ones in the base environment. For instance, if you have installed IPython into your environment, activating the environment allows you to write
ipython
to start IPython in the environment, rather than
/path/to/env/bin/ipython
In addition, environments may have scripts that add or edit other environment variables that are executed when the environment is activated (see the conda docs). These scripts can make arbitrary changes to the shell environment, including even changing the PYTHONPATH to change where packages are loaded from.
Finally, I wrote a very detailed answer of what exactly is happening in the code over there: Conda: what happens when you activate an environment? That may or may not still be up-to-date though. The relevant part of the answer is:
...the build_activate method adds the prefix to the PATH via the _add_prefix_to_path method. Finally, the build_activate method returns a dictionary of commands that need to be run to "activate" the environment.
And another step deeper... The dictionary returned from the build_activate method gets processed into shell commands by the _yield_commands method, which are passed into the _finalize method. The activate method returns the value from running the _finalize method which returns the name of a temp file. The temp file has the commands required to set all of the appropriate environment variables.
Now, stepping back out, in the activate.main function, the return value of the execute method (i.e., the name of the temp file) is printed to stdout. This temp file name gets stored in the Bash variable ask_conda back in the _conda_activate Bash function, and finally, the temp file is executed by the eval Bash function.
So you can see, depending on the environment, running conda activate python3_env && python my_script.py and ~/anaconda3/envs/python3_env/bin/python my_script.py may give very different results.

Conda: what happens when you activate an environment?

How does running source activate <env-name> update the $PATH variable? I've been looking in the CONDA-INSTALLATION/bin/activate script and do not understand how conda updates my $PATH variable to include the bin directory for the recently activated environment. No where can I find the code that conda uses to prepend my $PATH variable.
Disclaimer: I am not a conda developer, and I'm not a Bash expert. The following explanation is based on me tracing through the code, and I hope I got it all right. Also, all of the links below are permalinks to the master commit at the time of writing this answer (7cb5f66). Behavior/lines may change in future commits. Beware: Deep rabbit hole ahead!
Note that this explanation is for the command source activate env-name, but in conda>=4.4, the recommended way to activate an environment is conda activate env-name. I think if one uses conda activate env-name, you should pick up the explanation around the part where we get into the cli.main function.
For conda >=4.4,<4.5, looking at CONDA_INST_DIR/bin/activate, we find on the second to last and last lines (GitHub link):
. "$_CONDA_ROOT/etc/profile.d/conda.sh" || return $?
_conda_activate "$#"
The first line sources the script conda.sh in the $_CONDA_ROOT/etc/profile.d directory, and that script defins the _conda_activate bash function, to which we pass the arguments $# which is basically all of the arguments that we passed to the activate script.
Taking the next step down the rabbit hole, we look at $_CONDA_ROOT/etc/profile.d/conda.sh and find (GitHub link):
_conda_activate() {
# Some code removed...
local ask_conda
ask_conda="$(PS1="$PS1" $_CONDA_EXE shell.posix activate "$#")" || return $?
eval "$ask_conda"
_conda_hashr
}
The key is that line ask_conda=..., and particularly $_CONDA_EXE shell.posix activate "$#". Here, we are running the conda executable with the arguments shell.posix, activate, and then the rest of the arguments that got passed to this function (i.e., the environment name that we want to activate).
Another step into the rabbit hole... From here, the conda executable calls the cli.main function and since the first argument starts with shell., it imports the main function from conda.activate. This function creates an instance of the Activator class (defined in the same file) and runs the execute method.
The execute method processes the arguments and stores the passed environment name into an instance variable, then decides that the activate command has been passed, so it runs the activate method.
Another step into the rabbit hole... The activate method calls the build_activate method, which calls another function to process the environment name to find the environment prefix (i.e., which folder the environment is in). Finally, the build_activate method adds the prefix to the PATH via the _add_prefix_to_path method. Finally, the build_activate method returns a dictionary of commands that need to be run to "activate" the environment.
And another step deeper... The dictionary returned from the build_activate method gets processed into shell commands by the _yield_commands method, which are passed into the _finalize method. The activate method returns the value from running the _finalize method which returns the name of a temp file. The temp file has the commands required to set all of the appropriate environment variables.
Now, stepping back out, in the activate.main function, the return value of the execute method (i.e., the name of the temp file) is printed to stdout. This temp file name gets stored in the Bash variable ask_conda back in the _conda_activate Bash function, and finally, the temp file is executed by the eval Bash function.
Phew! I hope I got everything right. As I said, I'm not a conda developer, and far from a Bash expert, so please excuse any explanation shortcuts I took that aren't 100% correct. Just leave a comment, I'll be happy to fix it!
I should also note that the recommended method to activate environments in conda >=4.4 is conda activate env-name, which is one of the reasons this is so convoluted - the activation is mostly handled in Python now, whereas (I think) previously it was more-or-less handled directly in Bash/CMD.

Fedora environment variables

I'm very new to this so apologies if it doesn't make sense. I can't log into my computer (fedora 27) because I think I've incorrectly assigned an environmental variable called PATH.
I tried to set a path for an environmental variable by using
vi ~/.bashrc
PATH=usr/local/bin
once I exited vi the terminal would keep popping up with the question 'do you want to install sed? N/y'
I exited the terminal and started a new one but the same problem occurred.
I thought if I restarted my machine it may resolve itself but now I can't log back onto my personal user account. I input my password and it accepts it, starts to load the homepage but then asks for my password again. I can't think of what I did apart from set the local environment variable PATH.
I can log onto the root user so my question is does anyone know how to change the local environmental variable on my personal account while being logged onto the root user?
vi /home/user/.bashrc
Something like that?
Change the PATH Variable to something like this:
PATH=$PATH:usr/local/bin

Unable to Access ENV variable in rails

I am trying to use env variables in my rails app and set those variable values in ubuntu 14.04
I tried setting using export command
export mongodb_username="abc"
export mongodb_password="cde"
and also tried setting them in /etc/environment and in ~/.bashsrc
and printenv gives following results
>> printenv mongodb_username
=> abc
>> printenv mongodb_password
=> cde
BUT in RAILS APP or irb the output is following
>> ENV['mongodb_password']
=> nil
>> ENV['mongodb_username']
=> nil
I am missing something? Please help!!!
When setting an environment variable's value using export, that value is available only in the shell in which it was set, and its subshells. So you'll need to export those variables in every shell in which you need them.
However, you can automate this, of course.
If you have variables that you need frequently, one approach is to put their assignments in a shell script and then source the shell script in any shells you need them in (see http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x237.html for more on this).
If it's ok to have the variables be in effect in all your shells, then a simpler way is to export them from your startup script (probably ~/.bashrc).

Ubuntu: Environment variable is deleted after closing session

I am setting an environment variable in Ubuntu 14.04 for a script to use it.
I opened the terminal and did:
export VARNAME=/home/me/folder/folder2
And then run the script and everything works fine. But anyway as soon as I close my session, the variable seems to disappear and I have to declare it again like the first time.
To set an environment variable that doesn't get erased with the closing of the terminal (Ubuntu 16.04), follow these steps:
Open .bashrc file by using the text editor of your choice. For me, it was
code ~/.bashrc as I use VS Code, but you can use vi ~/.bashrc or subl ~/.bashrc.
Add the environment variable using export VARNAME=/home/me/folder/folder2
Save the file and close.
The variable will persist even after the terminal is closed.
Actually if you set the variable via terminal it will last till shutdown. If you want to set permanent variable you have to do the following.
$ vi ~/.bash_proflle
// set the variable in the file
exit by pressing esc key and type :wq Now the path is set.

Resources