Setting up Environment Variables in Mac OS

Setting up Environment Variables in Mac OS

Image for post

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.

Image for post

if you want to check a specific environment variable, you can do check it with command ?echo $variable_name?

Image for postPATH variable setting

2. Set an Environment Variable ? temporary or permanent

you can set an environment variable for the temporary 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

Image for postSet a temporary environment variable using export command

And then close the terminal and open another one to check out if the set variable has disappeared or not.

Image for postTemporary Variable is gone now.

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, ~/.bash_profile, ~/.bashrc.

For system-wide operations, it should be in /etc/profile,

For user based operations, it should be in ~/.bash_profile,

For non-login interactive shells, it should be in ~/.bashrc.

(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.

Image for postEditing .bash_profile file with nano editor.Image for postImage for postOnce refresh environment variable with ?source? command, the current shell can locate the new directory for executable binary files.Image for postImage for postAfter removing export line in .bash_profile, then source it, and reopen the terminal.

In the end, I have successfully practiced permanent setting. This is going to be useful for any development environment setting hereafter.

Thanks!

16

No Responses

Write a response