Environment Variables

Cerosh Jacob
Nov 17, 2020 · 5 min read

Environment variables are set to allow access to command line tools and to enable other tools to interact with SDKs more easily. For example, if the environment variable is not set on the PATH, running adb command is much more cumbersome as the user has to type full path to the command each time in the terminal. Knowing to set environment variable is an inevitable task for successful test automation. There are load of links available but most of them are outdated at least in case of macOS. Being a mobile automation engineer this document is depicted for users having macOS Catalina and above.

An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. During application initialization, environment variable are loaded into process.env and accessed by suffixing the name of the environment variable. The environment variable is implemented as strings that represent key-value pairs. Just like any other variable at runtime, the reference to the environment variable name is replaced with its current value. Environment variables can also be used to store temporary values for reference later in a shell script. The most well-known environment variable is PATH which contains the paths to all folders that might contain executables.

export PATH=$PATH:/usr/local/bin

When multiple values needs to be passed, they are separated by colon (:) characters. Defining a variable is done with a name but for accessing the variable a $ sign has to be prefixed with the variable name. In the above command new PATH variable will be created by appending usr/local/bin to the existing $PATH variable.

Before jumping into how to set environment variables few insights about shell scripts as the environment variable are set through shell scripts. A shell script is a file which contains commands. The shell read the files and execute the commands using terminal in the order listed in the file. So in short most of things which can be done through a terminal can be done using a shell script or vice versa. Mac uses either zsh (Z shell) or bash (Bourne-again Shell) as the shell or command-line interpreter. I was forced to create this document because with the latest macOS update (macOS Catalina), Mac has made Z shell as the default login shell and interactive shell across the operating system. Most of google search for setting environment variable in Mac still lands in pages using bash shell and takes to the no mans land. The Z shell is a UNIX shell that is built on top of bash with additional features. So has to relay on .zshrc files instead of the .bashrc or .bashprofile files for setting up environment variables. So this document is an attempt to capture the basic steps to create an environment variable.

The most common commands that are used in shell scripting are echo and export.
Echo is built-in command that typically used in scripting language and batch files to display a line of text/string on standard output or a file. Echoing the built-in variable shell using $SHELL will display the shell used by the current user.

ceroshjacobs@Ceroshs-MacBook-Pro ~ % echo $SHELL
/bin/zsh

The export command in shell makes the variable corresponding to the specified names available for the subsequently executed command. During variable declaration the value following the equal sign will be assigned to the variable. Export command will display all the exported variable for the current user.

ceroshjacobs@Ceroshs-MacBook-Pro ~ % export
ceroshjacobs@Ceroshs-MacBook-Pro ~ % export HELLO=world
ceroshjacobs@Ceroshs-MacBook-Pro ~ % echo $HELLO
world

The above example declares ‘HELLO’ as a variable and assigns ‘world’ as its value. Echoing the newly create variable $HELLO will display its value in the terminal. This variable can exist only for that session and user hence this value will not be retained with a new session. This is where environment variable comes to the rescue. Mentioned previously that environment variable are created in shell script more specifically start-up scripts. Shell programs uses different types of start-up script to create a shell environment. E.g. zprofile, zlogin, zshrc, etc. Each script has a specific use and affects the environment differently hence the existence of several start up scripts. Every subsequent script executed can override the values assigned by previous scripts.

From a test automation perspective environment variables has to be included in the .zshrc start up script. The dotfiles name is derived from the configuration files in Unix-like systems that start with a dot (e.g. .bash_profile and .gitconfig). For normal users, this indicates these are not regular documents, and by default are hidden in directory listings.

In Mac command+shift+. Will display the hidden files. The .zshrc file lives in the home directory

ceroshjacobs@Ceroshs-MacBook-Pro ~ % ls $HOME/.zshrc
/Users/ceroshjacobs/.zshrc

the above list command will confirm the existence of .zshrc file by displaying the full location of the file if it exist else will display no such file or directory message.

ceroshjacob@Ceroshs-MBP-2 ~ % ls $HOME/.zshrcls: /Users/ceroshjacob/.zshrc: No such file or directory

If the script doesn’t exist a blank script can be created using the touch command. If it exist the new changes can be appended at the bottom of the script.

ceroshjacobs@Ceroshs-MacBook-Pro ~ %touch /.zshrc

Based on the above understanding lets set the environment variable for Java in the .zshrc script. When setting the environment variable for Java two things has to be taken care. First provide the location to the java development kit to convert the java source code into the machine-readable binary format. Then share the location for Tools like javac and java. For users who are not sure of the location from Mac OS X Leopard, Apple introduced a command line tool which dynamically finds and displays the top Java version specified in Java Preferences for the current user.

ceroshjacobs@Ceroshs-MacBook-Pro ~ % /usr/libexec/java_home 
/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home

The word jdk appears in the displayed location. Want to reiterate that the location of jdk is to be provided and not the jre. When there is space in the file location surrounding by quotes is the option if not still putting the location between quotes won’t harm. So including the below command in the shell script will create the environment variable JAVA_HOME.

Export JAVA_HOME=”/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home”

The environment variables are set when opening a new shell session. So when introducing a new environment variable the shell has no way of picking the change and hence has to restart the terminal for the change to reflect. Echoing the variable will display the jdk location.

ceroshjacobs@Ceroshs-MacBook-Pro ~ % echo $JAVA_HOME 
/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home

After setting environment variable to java development kit location the next part is to set location for the java tools which are located in the bin folder under the JAVA_HOME variable. Shell script allows referring the $JAVA_HOME variable and then point towards the bin sub folder. Since the $JAVA_HOME environment variable is referred this command should be included only after creating the JAVA_HOME variable. Even when creating .zshrc for the first time it’s advisable to create new PATH variable by appending to the existing $PATH variable. As there may be some $PATH variables included through some other start up scripts.

export PATH=$PATH:$JAVA_HOME/bin

Again restart the terminal and type javac or java displaying the various options will confirm the success of setting the environment variable.

After including the path of chromedriver location to $PATH mac may through a security warning. In that case, need to add the below command to allow access.

xattr -r -d com.apple.quarantine chromedriver

Learn cool stuffs like this and more @HeadSpinUniversity from the master directly Jonathan Lipps through his course https://bit.ly/3iLAWt1

The Startup

Medium's largest active publication, followed by +775K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store