- Setting up Environment Variables in MacOS Sierra
- Use environment variables in Terminal on Mac
- Setting up Environment Variables in Mac OS
- 1. Check the current environment variable settings.
- 2. Set an Environment Variable — temporary or permanent
- Where to Set Environment Variables in Mac OS X
- Displaying Current Environment & Shell Variables in Mac OS X
- Setting Environmental Variables in Mac OS X Command Line
- Setting Temporary Environmental Variables in OS X
- HowTo: Set an Environment Variable in Mac OS X — Terminal Only
- Login shell vs non-login shell
- Global PATH only
- /etc/paths.d/ec2-api-tools
- Setting environment variables
- Terminal — hostname:
- Login shell
Setting up Environment Variables in MacOS Sierra
Jun 21, 2017 · 2 min read
An environment variable in a named object containing data which can be used by multiple applications or processes. Basically, it is just a variable with a name and an associated value. It can be used to determine anything like location of executable files, libraries, current working directory, default shell, or local system settings.
For those new to mac can get overwhelmed with how to set up and manage these environment variables. This guide provides easy ways to do so.
Displaying current Environment Variables
This is very easy. Just open the Terminal and run the command printenv as shown below.
T h is will list all the environment variables currently set.
However, for displaying the value of any specific environment variable run the echo $[variable name] on the terminal, as shown below.
Setting temporary environment variable using terminal
If the environment variable you wish to set is to be used once or twice, you would like to set a temporary variable for it, avoiding unwanted variables staying in the system. You can do this simply by opening the terminal and running export command followed by the variable name and its value.
The above example sets the variable $JAVA_HOME to the specified value. However, if your requirement is to append a value to an existing environment variable, then assign the value as
the ‘:’ here append the value to the existing value. See example below.
Setting permanent environment variable using terminal
Since Mac uses bash shell, so the environment variables can be added to the .bash_profile directory, for the current user. The path to this file can be found using the command
Get started by opening this file using a text editor. I’m using nano — a terminal based text editor, you may use any text editor you like — to open the file and edit it.
This will open the .bash_profile file in the terminal.
Note: If there is no file named .bash_profile , then this above nano command will create a new file named .bash_profile .
Now move to the end of the file, go to the next line. Now add the desired environment variables using export command as we did before.
Press ctrl+X to exit the editor. Press ‘ Y ’ for saving the buffer, and you will return back to the terminal screen.
We are done now!
You may again run echo $[variable_name] to see the value of your just saved environment variable.
UPDATE: Don’t forget to close and reopen the terminal before using your newly set environment variable. Reopening the terminal loads the updated .bash_profile file.
Источник
Use environment variables in Terminal on Mac
The shell uses environment variables to store information, such as the name of the current user, the name of the host computer and the default paths to any commands. Environment variables are inherited by all commands executed in the shell’s context and some commands depend on environment variables.
You can create environment variables and use them to control the behaviour of a command without modifying the command itself. For example, you can use an environment variable to have a command print debug information to the console.
To set the value of an environment variable, use the appropriate shell command to associate a variable name with a value. For example, to set the variable PATH to the value /bin:/sbin:/user/bin:/user/sbin:/system/Library/ , you would enter the following command in a Terminal window:
To view all environment variables, enter:
When you launch an app from a shell, the app inherits much of the shell’s environment, including exported environment variables. This form of inheritance can be a useful way to configure the app dynamically. For example, your app can check for the presence (or value) of an environment variable and change its behaviour accordingly.
Different shells support different semantics for exporting environment variables. See your preferred shell’s man page.
Although child processes of a shell inherit the environment of that shell, shells are separate execution contexts that don’t share environment information with each other. Variables you set in one Terminal window aren’t set in other Terminal windows.
After you close a Terminal window, variables you set in that window are no longer available. If you want the value of a variable to persist across sessions and in all Terminal windows, you must set it in a shell startup script. For information about modifying your zsh shell startup script to keep variables and other settings across multiple sessions, see the “Invocation” section of the zsh man page.
Источник
Setting up Environment Variables in Mac OS
Apr 21, 2018 · 3 min read
As I am getting used to MAC OS instead of Windows, I felt the need to make myself fully understood how to manage environment variables in MAC.
1. Check the current environment variable settings.
you can list up by command “printenv” in the console.
if you want to check a specific environment variable, you can do check it with command “echo $variable_name”
2. Set an Environment Variable — temporary or permanent
you can set an environment variable for the t e mporary or permanent use. It depends on the case, if you need a variable for just one time, you can set it up using terminal. Otherwise, you can have it permanently in Bash Shell Startup Script with “Export” command.
1) Temporary Setting
And then close the terminal and open another one to check out if the set variable has disappeared or not.
2) Permanent Setting
For permanent setting, you need to understand where to put the “export” script. Where here means Bash Shell Startup Script like /etc/profile,
For system-wide operations, it should be in /etc/profile,
For user based operations, it should be in
For non-login interactive shells, it should be in
(For better understanding, you better check out this:Unix Introduction — Shell)
But the convention above doesn’t exactly apply to MAC OS. In Unix/Linux for the interactive login shells, ./bash_profile opens only at the first time in logging in and /.bashrc gets loaded thereafter. But MAC OS only loads ./bash_profile even after the first terminal.
For experiment, I am going to add a test directory to the PATH environment variable. Using “export” command, the PATH variable is going to hold the newly added directory.
Источник
Where to Set Environment Variables in Mac OS X
At the command line, environmental variables are defined for the current shell and become inherited by any running command or process. They can determine anything from the default shell, the PATH, the users home directory, to the terminal emulation type, current working directory, where a history file is located, language and localization settings, and going further to include shell variables, which include everything from customizations to the bash prompt, colorized ls output, and changes to terminal appearance, to aliases, and much more.
Let’s walk through how to list environment and shell variables, and then how to set and add new environment variables at the command line of Mac OS X.
Displaying Current Environment & Shell Variables in Mac OS X
To quickly get a list of environmental variables, you can use the following command:
If you want to see a complete list of shell variables, the ‘set’ command can be issued as well:
The output of these commands can be lengthy so you may wish to pipe the output through the less or more commands.
Setting Environmental Variables in Mac OS X Command Line
Because the Mac defaults to using bash shell, you can set environmental variables in the user directories .bash_profile, for an active user account the path to that file is locate at:
If you changed your shell or you aren’t sure what shell you’re using, you can always check by issuing the echo $SHELL command, which will display which shell is in use. We’re going to assume you’re still using the OS X default bash shell, thus we’ll add new environment variables by modifying .bash_profile with nano – you can use vi, emacs, or another text editor if you’d like, but we’ll cover nano for it’s simplicity.
Start by opening .bash_profile within the nano text editor:
You can add environmental variables and shell variables onto new lines, if there is already data within the .bash_profile file, just be sure to add new variables to a new blank line by using the arrow keys and the return key as necessary.
Let’s take an example and say we’re going to set the JAVA_HOME and JRE_HOME environmental variables within .bash_profile by adding the following to new lines of the file:
export JAVA_HOME=$(/usr/libexec/java_home)
export JRE_HOME=$(/usr/libexec/java_home)
Assuming we’re now finished, save changes made to .bash_profile by hitting Control+o (that’s an o as in otter), then exit out of nano by hitting Control+X
Changes and additions made to environmental variables will require the shell to be restarted or a new shell to spawn.
Setting Temporary Environmental Variables in OS X
It’s worth mentioning that you can also set temporary environmental variables in bash by using the ‘export’ command by itself, though these will only persist for as long as the current bash shell remains active. For example, if you wanted to add a temporary path to
/bin/ you could use the following command:
Again, the ‘export’ command run by itself and not contained within .bash_profile will only be a temporary setting and the environmental variable will not persist unless you add it to the .bash_profile.
If you’re actually looking add a new PATH for usage, you should almost certainly add it to .bash_profile by placing the appropriate export command into the file.
Going beyond bash shell, if you changed your Terminal app default shell away from bash to tcsh, zsh, sh, ksh, fish, or any of the other alternate shells out there, you’ll simply need to modify the appropriate profile or rc file for that specific shell (.tschrc, .cshrc, .profile, etc).
Источник
HowTo: Set an Environment Variable in Mac OS X — Terminal Only
The process of setting environment variables in the Terminal will be familiar to those with experience with Linux or UNIX. It involves editing files in /etc for global environment variables, and in your home directory for user specific environment variables.
Login shell vs non-login shell
A login shell is the shell that is started when you open a new terminal window. A non-login shell is when you start a sub-shell in the window, for example, typing bash when you have the bash shell running already.
Global PATH only
If you just want to edit the global PATH variable, you can put a file in /etc/paths.d with the path you want to add. For example, we will create a file /etc/paths.d/ec2-api-tools and put the following in:
/etc/paths.d/ec2-api-tools
Open a new Terminal and $PATH will reflect the change.
Setting environment variables
The format for setting an environment variable in the bash shell is KEY=value with no spaces around the equal sign. In addition, when putting it in a configuration file it is necessary to prepend it with export . The following is an example of how to set an environment variable in one of the configuration files, it also works on the command line; we will set the variable JAVA_HOME to /Library/Java/Home :
Terminal — hostname:
Login shell
First /etc/profile , then whichever of the following exists, trying them in order:
Источник