- Setting up Environment Variables in Mac OS
- 1. Check the current environment variable settings.
- 2. Set an Environment Variable — temporary or permanent
- Use environment variables in Terminal on Mac
- 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
- How to Set Environment Variables in MacOS
- Check Current Environment Variables
- List All Environment Variables
- Check A Specific Environment Variable
- Set Temporary Environment Variable
- Set Permanent Environment Variable
- Unset Environment Variable
- Setting up Environment Variables in MacOS Sierra
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.
Источник
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.
Источник
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).
Источник
How to Set Environment Variables in MacOS
Home » SysAdmin » How to Set Environment Variables in MacOS
Environment variables are used to store system-wide values that can be used by any user and process under the operating system. Setting environment variables is essential in the steps of certain installations we covered such as How To Install Java or How To Install Java On Raspberry Pi.
This tutorial explains how to view, add, and remove temporary and permanent environment variables on macOS.
- A system running the lastest version of macOS
- Access to the terminal
- A user with admin-level privileges
Check Current Environment Variables
There are two ways to check current environment variables in macOS:
1. Display and review a list of all current environment variables.
2. Display and review a specific environment variable.
List All Environment Variables
Use the printenv command to display a list of currently set environment variables:
Note: If you want to display the complete list of shell variables, use the set command.
Check A Specific Environment Variable
If you want to display the value of any specific environment variable, use the echo command:
For example, to check the value of the PATH variable which stores a list of directories with executable files, use the
echo command:
Note: Always use the $ prefix when specifying a variable name.
Set Temporary Environment Variable
The value you assign to a temporary environment variable only lasts until you close the terminal session. This is useful for variables you need to use for one session only or to avoid typing the same value multiple times.
Assign a temporary environment variable with the export command:
- [variable_name] : The name for the new temporary environment variable you want to set.
- [variable_value] : The value you want to assign to the new variable.
The export command also allows you to add new values to existing environment variables:
- [existing_variable_name] : The name of the environment variable you want to add a new value to.
- [new_variable_value] : The value you want to add to an existing variable.
For example, if you want to add a custom folder path to the PATH variable, use:
Set Permanent Environment Variable
Permanent environment variables are added to the .bash_profile file:
1. Find the path to .bash_profile by using:
2. Open the .bash_profile file with a text editor of your choice.
3. Scroll down to the end of the .bash_profile file.
4. Use the export command to add new environment variables:
5. Save any changes you made to the .bash_profile file.
6. Execute the new .bash_profile by either restarting the terminal window or using:
Unset Environment Variable
Use the unset command to remove an environment variable:
After following this tutorial, you should know how to set temporary and permanent environment variables in macOS. This should make it easier for you to configure software packages in the future.
Источник
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.
Источник