Environment variable is not set MacOS [duplicate] - macos

This question already has answers here:
Mac OS X 10.9 - setting permanent environment variables
(8 answers)
Closed 6 months ago.
I need to set environment variables to build an Ionic application, open the terminal and run the command:
export ANDROID_SDK_ROOT=$HOME/Android/Sdk
Without closing the terminal I type echo $ANDROID_SDK_ROOT, and the environment variable is returned perfectly, so I run "source ~/.bash_profile", to update the environment variables. I open and close the terminal and my environment variable is empty!
I tried to set it manually by opening the environment variables files, with a text editor but without success.

For macOS 10.15 Catalina and Newer, you need to use a .zshenv file instead of .bash_profile. This is because, by default since Catalina, the terminal uses zsh instead of bash.
Export paths permanently in the following manner:
Create .zshenv file:
touch ~/.zshenv
open -a TextEdit.app ~/.zshenv
Type out the export you want to do in this format:
export ANDROID_SDK_ROOT=$HOME/Android/Sdk
and save it. (From this old answer)

Related

How do programs add to $PATH variable with using .bash_profile?

How do some programs seem to add to the $PATH variable without using a .bash_profile file? What are the advantages to these approaches of adding to $PATH variable, but not using a .bash_profile file?
For my mac
echo $PATH returns
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin
which java returns
/usr/bin/java
which tex returns
/Library/TeX/texbin/tex
How is Java available in the terminal without being in the $PATH variable? (This is an example of something running in the terminal without being in the $PATH variable.)
How did TeX add to the PATH variable without using a .bash_profile file?
What added Library/Apple/usr/bin to the PATH variable? Wikipedia says that only the first three are defaults: /bin, /usr/bin, and /usr/local/bin.
Note: Other StackOverflow posts helped users create a .bash_profile file (a file that doesn't exist by default on a Mac) to run programs such as android and adb when they received -bash: android: command not found
My question is about how the terminal works (like how java is running without being on $PATH) and how other programs (like TeX) added to $PATH without using a file like .bash_profile.
I am running macOS 10.15.5, confirmed that I do not have a .bash_profile file, do have homebrew installed (not sure if that affects anything) and when I open a terminal, it says "The default interactive shell is now zsh." (I think I installed zsh after reading a different StackOverflow a few weeks back.

Environment Variables macOS & WIndows

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""

How to set custom Environment variable on Mac?

I have a requirement where I need to set a custom environment variable called CLUSTER_ENV = '#fooURL'
The steps I have followed are:
Open terminal, open bash_profile and save CLUSTER_ENV='#foo'. When I do echo $CLUSTER_ENV , I get blank output.
I did the same thing in the bashrc file and in this case, the $CLUSTER_ENV shows the value only when I run it in the same terminal window.
Which is the best or recommended way to permanently set the environment variable on a Mac?
Im running El Capitan.
I have gone through these links for reference:
http://osxdaily.com/2015/07/28/set-enviornment-variables-mac-os-x/
Mac OS X 10.9 - setting permanent environment variables
As you will normally do on any Linux distro: export CLUSTER_ENV=my.url.com which will have to be added to ~/.bash_profile

How to set path variables on osx el capitan?

I've not had success setting up an environment variable on El Capitan. Nothing to do with dock or bash shell. Trying to run a java gui which requires an environment path variable to a JDBC driver to connect to an oracle db:
http://docs.oracle.com/cd/E11882_01/install.112/e38228/inst_task.htm#BABBBHJH
I have tried methods:
in launchd.conf:
setenv JDBC /Users/mac_admin/Downloads/Oracle/instantclient_12_1
osx - Setting environment variables in OS X? - Stack Overflow
in .profile:
export JDBC /Users/mac_admin/Downloads/Oracle/instantclient_12_1
terminal - How do I set environment variables on OS X? - Ask Different
in .bash_profile:
export JDBC=$(/Users/mac_admin/Downloads/Oracle/instantclient_12_1)
Where to Set Environment Variables in Mac OS X
In each case, the export command at Terminal does not show the new variable.
What is correct method for El Capitan?
Is "JDBC" an acceptable name for the variable? Or am I supposed to label it PATH? I notice the export command already lists a PATH item, and I am afraid to overwrite it:
declare -x PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Frameworks/Mono.framework/Versions/Current/Commands"
Is /Users/mac_admin/Downloads/Oracle/instantclient_12_1 an acceptable location for the files?
thx
It kind-of matters where you want to use this. If you are just trying to get some binary utility to run from Terminal shells you launch add an export into ~/.bash_profile and you are good to go: export JDBC='Robert was here'
tcc-rkuhar:scouting robert.kuhar$ source ~/.bash_profile
tcc-rkuhar:scouting robert.kuhar$ echo $JDBC
Robert was here
You need the equals sign to get the environment variable set. Assuming that the value you've put in the export JDBC is a directly that has the binaries you want to execute. The line in your ~/.bash_profile is probably...
export JDBC=/Users/mac_admin/Downloads/Oracle/instantclient_12_1
PATH="${PATH}:$JDBC"
You'll know this worked if you
echo $PATH
/usr/local/bin:...blah...blah...blah::/Users/mac_admin/Downloads/Oracle/instantclient_12_1

restore PATH under Mac OS X [duplicate]

This question already has an answer here:
How to restore .bash_profile on a mac? None of my unix terminal are working [closed]
(1 answer)
Closed 6 years ago.
Hi guys I tried to add new directory to the PATH, but instead appending I overwrote it. Is there any way to restore those default paths?
If you changed your PATH in a Terminal shell, simply close that Terminal window and open a new one. Changes to environment variables are local to the shell in which you change them (and any subshells created by that one).
Just restart your terminal that will assign variables based on your .profile or .bashrc ( if you are running bash )

Resources