zsh

How Do I Set An Environment Variable in ZSH


If you have ever used the Linux terminal, you are probably aware of environment variables. Environment variables refer to a set of dynamic variable names that store a value that applications from the Linux shell can use.

Environment variables come as key-value pairs where the key represents the variable’s name and a value. You can customize the values of environment variables, with the changes reflected globally on all the applications launched from the shell.

This guide will discuss how to interact with environment variables, reading, and setting environment variables using the ZSH shell.

How Environment Variables Work

Let us take a moment to understand how environment variables work.

In Linux and other Unix-based systems, every time we launch a new shell session, it initializes a process to gather the information that should be available within that shell session and all its child processes. Such information is gathered from configuration files such as .zshrc, .zlogin, etc.

The purpose of environment variables is to provide a simple medium where the shell can fetch or set settings related to the system.

Environment variables take the following format:

KEY=value

KEY=value1:value2

If the environment variable contains multiple values, each value is separated by a colon (:). A famous example of such an environment variable is the $PATH.

Similarly, if the value of the environment variable contains spaces, the value is enclosed in a pair of quotation marks as:

KEY="value with spaces"

Environment variables are case-sensitive. It is recommended to use UPPERCASE names for environment variables to distinguish them within other contexts.

You should not include spaces around the equal sign (=).

There are two main types of variables:

  1. Environment Variables
  2. Shell Variables.

Environment Variables

Environment variables are global variables inherited by all shells and their corresponding child processes.

Shell Variables

On the other hand, Shell variables are available within the shell in which they are defined or set. They are commonly used to store temporary data and are automatically destroyed once the shell session is terminated.

Each shell such as Bash, ZSH, fish, and others contain their own set of shell variables.

How to Print Environment Variables

Various Linux shells keep track of their predefined shell and environment variables. We can view these variables using various commands.

The most common command to view the environment variables is the printenv command. If you run this command with no arguments, it prints all the environment variables as shown in the example below:

printenv

The command above should print all the environment variables as shown:

SHELL=/bin/zsh

SESSION_MANAGER=local/debian10.local:@/tmp/.ICE-

unix/964,unix/debian10.local:/tmp/.ICE-unix/964

QT_ACCESSIBILITY=1

COLORTERM=truecolor

XDG_MENU_PREFIX=gnome-

GNOME_DESKTOP_SESSION_ID=this-is-deprecated

SSH_AUTH_SOCK=/run/user/1000/keyring/ssh

DESKTOP_SESSION=gnome

GTK_MODULES=gail:atk-bridge

XDG_SEAT=seat0

PWD=/home/debian

XDG_SESSION_DESKTOP=gnome

LOGNAME=debian

XDG_SESSION_TYPE=wayland

GJS_DEBUG_TOPICS=JS ERROR;JS LOG

GDM_LANG=en_US.UTF-8

HOME=/home/debian

USERNAME=Debian

---------OUTPUT TRUNCATED-------------------------------------

Using the printenv command, you can print an individual variable by passing it as an argument. For example, to show the value of the $PATH, you can use the command:

printenv PATH

The command should return the value of the PATH variable as:

/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

You can also pass multiple variables to the printenv command as:

$ printenv PATH HOME USERNAME

/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

/home/debian

debian

The command should print the values of the environment variables passed each in one line.

TIP: You can also use the env command to print or set the values of environment variables.

Popular Environment Variables

The following are some standard environment variables.

  • HOME – Represents the home directory for the current user.
  • SHELL – Shows the path of the current user’s shell.
  • USER – Shows the currently logged-in user.
  • PATH – Contains a list of directories to be searched for executable files when a command is executed.
  • LANG – Shows locale settings, including character encoding
  • TERM – Refers to the current terminal emulation.
  • _ – Shows the previously executed command for that user.

How to Print Shell Variables

Both the printenv and env commands will only show the environment variables. Use the set command to view all the variables, including shell and local variables, functions, and environment variables.

If you execute the set command without any parameters, it will print all the variables as:

set

Here is an example output:

'!'=0

'#'=0

'$'=2801

'*'=(  )

-=569JNRXZghiklms

0=zsh

'?'=0

@=(  )

ARGC=0

BG

CDPATH=''

COLORTERM=truecolor

COLUMNS=211

CPUTYPE=x86_64

DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/1000/bus'

DESKTOP_SESSION=gnome

DISPLAY=:0

EGID=1000

EPOCHREALTIME

EPOCHSECONDS

EUID=1000

FG

FIGNORE=''

FPATH=/home/debian/.oh-my-zsh/plugins/git:/home/debian/.oh-my-

zsh/functions:/home/debian/.oh-my-zsh/completions

---OUTPUT TRUNCATED----------------------------------------

The values printed from the set command are typically a massive list of items you can filter by piping the output to commands such as grep.

Popular Shell Variables

Although shell variables will vary depending on the shell you are using; the following are common in the ZSH shell.

  • ZSH_VERSION – The version of the ZSH shell being executed.
  • PROMPT – Shows the placeholders for the current ZSH prompt format. Similar to $PS1.
  • HISTSIZE – Number of history commands stored in the memory.
  • HISTFILE – path to the ZSH command history file.
  • ZSH_NAME – ZSH shell name.
  • UID – UID of the current user.
  • IFS – Stores the value of the internal field separator.

How to Set Environment Variables in ZSH

To set environment variables, we use the export command. The syntax is:

export KEY=value

For example, to create a http_proxy variable and set it to a URL, we can do:

export HTTP_PROXY="http://127.0.0.1:8080"

To verify the creation of the variable, we can use the printenv command as:

printenv HTTP_PROXY

http://127.0.0.1:8080

Once you set an environment variable, you can access it from all the child processes. For example:

zsh -c 'echo $HTTP_PROXY'

However, environment variables created in the method discussed above are only available within the current shell session. Once closed, all the environment variables are destroyed.

How to Create Permanent Environment Variables

If you want to create an environment variable that can persist across shell sessions, you can use the shell configuration files.

In ZSH, you can define the environment variables in the .zshrc file.

Similarly, you need to use the export command as:

vim ~/.zshrc

export HTTP_PROXY-"http://127.0.0.1:8080"

Close and save the file.

To load the environment variables in the current shell session, use the source command as:

source ~/.zshrc

Conclusion

In this guide, we discussed how to work with environment and shell variables in Linux systems. We also learned how to set environment variables and apply persistency across shell sessions.

Thank you for reading.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list