OSX Catalina & zsh as my terminal.
I have a command to start a psql connection :
db, err := pgx.Connect(context.Background(), os.Getenv("PSQL_URL"))
But the os.Getenv("PSQL_URL") is an empty string.
How to make sure go program can read my environment variable ?
In the terminal, if I echo $PSQL_URL I get the proper postgresql://aod:toto#localhost/dbname
If I export PSQL_URL="postgresql://aod:toto#localhost/dbname" before running main.go it works fine
I'm looking for a persistent way of doing it.
UPDATE
My mistake was as follow :
Inside ~/.zhsrc I did set
PSQL_URL="postgresql://aod:toto#localhost
instead of
export PSQL_URL="postgresql://aod:toto#localhost"
That's the correct way of reading an env variable in go.
The only thing that comes to my mind about what might be happening is that you have two different terminal sessions. In one of them you set PSQL_URL (you are running the echo in this one) and in the other not (you are running your go app here).
EDIT
Expanded now with the your comments and Felix's, the problem here is that you export a variable, but don't make it permanent, so when you start a new session the variable doesn't exist. What you need to do is edit your ~/.zshrc and add export PSQL_URL=foo, this way, whenever you start a new terminal session, the variable will be loaded.
Related
In Windows' Environment Variable Tab, the User Variable and System Variables are separated. I have a MacBook Pro and would like to add new 'user' variables like Windows. Is this possible? I mean I found there are temporary variables that can be created in the Terminal, but if that Terminal is closed, the variables are gone. I found a way to do a permanent variable, but do not want to mess things up in the core of the macOS. Are there any recommendations?
If you set variables in the ~/.bash_profile file, they will always exist since this file is automatically run every time you open Terminal.
To edit it:
vim .bash_profile
(i to insert text, esc to exit insert mode, :q! to discard and quit, :x to save and quit)
Then add:
export VARIABLE_NAME=value
Save and quit the editor (:x). Then to check if it worked:
echo $VARIABLE_NAME
When the terminal is bash, editing the .bash_profile works.
If you're using zsh, then edit
~/.zsh and add the command as
""export variableName = value"" in to the file
Now, run
""source ~/.zsh""
I have 4 files in my bin. Funnily, two of them work when I call them in the terminal - the other (newer) two don't.
My bin file looks like this: https://ibb.co/bsj00jG
When I type 'which chd-project' in terminal (chd-project is one of the bash scripts which works), it says /usr/local/bin/chd-project - however I can't find a local file on my Mac.
When I type which id-project (the bash script that can't be found), it just says id-project not found.
If I set PATH=$HOME/bin, I can then call the id-project file. However, whenever I restart my terminal, it resets again. This can sometimes be buggy, though, as later commands in that same bash script can sometimes not be found.
When I type echo $PATH I get /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
However, like previously stated, I can call chd-project in my terminal (although it says usr/local/bin if i use 'which') but I can't call id-project.
Any help would be greatly appreciated.
Thanks
Your PATH variable is "reset" for every session you start. That's because the current session doesn't set environmental variables persistently. However, before a session is started it executes files that, for example, hold the value of PATH.
If you want to add this for every terminal you open, you should extend your path in your bash profile.
$ echo "export PATH=$PATH:$HOME/bin" >> $HOME/.bashrc
Please take not that you shouldn't overwrite your PATH variable, because it's used to find commands like mv, cp, etc.
EDIT:
I don't know Atom that well, but if you would open a regular (not an in-IDE) terminal it should work. It could be that Atom doesn't execute .bashrc for whatsoever reason. You could try to add it to to your profile.
$ echo "export PATH=$PATH:$HOME/bin" >> $HOME/.profile
I am trying to set a global/system variable in my computer, I am using Yosemite.
I opened my ~/.profile (that is the default profile file configuration) , and I added this
setenv var.Property someValue
but it didnt work,
Neither this way
export var.Property = someValue
everytime I do echo $var.Property I get as result "Property" I think the "." may be interfering with this. I do need to have it this format, but unfortunately, is not working.
Any idea how to do this?
You will get a different environment based on if you start via command line vs double clicking ... if you start with the command line you should get whatever is in env and if you start by clicking it then you will get whatever is in launchtl's env... so for that to change you use
launchctl setenv DOG cat
Edit:
it seems as though you can't have an env var with a key that has a period in it... this works fine in csh:
setenv var.Property someValue
I've been having proxy issues with npm and git.
Looks like the proxy is set as an environment variable in Bash
If I check with
env | grep -i proxy
I can see the proxy settings.
I can unset the proxy with
unset http_proxy
unset https_proxy
unset ftp_proxy
This only appears to last as long as the terminal window is open.
If I close and reopen the terminal the proxy is back again.
Is is it possible to delete the proxies when I'm out of office and then recreate them when I need them?
You first need to find out where these environment variables are defined in your case.
Typically they are set in the ~/.bash_profile file (Mac OS X) or ~/.bashrc file (Linux). Since you tagged the question with osx, I assume that in your case they are set in ~/.bash_profile.
Please check whether this file defines these variables. You can either do that using Finder (you need to show hidden files), or from a terminal by running cat ~/.bash_profile. Feel free to post the output in your question.
There's a small chance that they are defined in a system-wide file like /etc/bashrc. Feel free to check that file as well.
If you need to define/undefine the variables on a regular basis, I recommend that you create a script for each defining/undefining and place it in a location like ~/bin. Then you can simply call the respective script when you open a new shell.
I handle this through the Bash-it framework. In my fork, I have added a proxy plugin that provides functions for this. Feel free to give this a try: https://github.com/nwinkler/bash-it/blob/master/plugins/available/proxy.plugin.bash
To answer your other question: There's no easy way to automatically detect and change the presence of a proxy from a shell window.
I am trying to download myrna tool on mac os x.
for one of procedures on installation, "Set the MYRNA_HOME environment variable to point to the extracted directory (containing myrna_emr)"
So i tried this way
echo "export MYRNA_HOME=/.../....directory/.." >> ~/.bashrc
but it seems it does not work.(when i type echo MYRNA_HOME in command line, there is nothing OR when i type printenv MYRNA_HOME does not come out.)
anyone who knows how to set up Env Vars??
thank you,
There are several ways to do that. One would be adding your variable to ~/.bash_profile file.
Example:
MYRNA_HOME=/whatever/you/want/
export MYRNA_HOME
Then you can try if it works by logging in again and trying on a terminal:
$ echo $MYRNA_HOME
It should print whatever you set MRYNA_HOME to on ~/.bash_profile.
If you want the environment variable to be available to all users, you can change /etc/bashrc file. But I would prefer just changing one users' environment.