Variables

Background

Variables are implemented as key/value strings. Colons (:) are used to separate multiple values.

KEY=value1:value2:value3

KEY="value1 with spaces":value2
  • Keys are always uppercase.
  • Use colons : to separate multiple values.
  • Use quotes "a b c" around values with white-space.

Keys can represent environmental or shell variables.

Environmental variables are variables that are defined for the current shell and are inherited by any child shells or processes. Environmental variables are used to pass information into processes that are spawned from the shell.

Shell variables are variables that are contained exclusively within the shell in which they were set or defined. They are often used to keep track of ephemeral data, like the current working directory.

# Show all environment variables and values
env

# Show the value of a specific environment variable
echo ${VARIABLENAME}

PATH

The PATH environment variable lists directories.

On Unix systems, programs (command-line utilities like cp or rm) are kept in many different places. When you execute a program your shell searches the PATH directories for it. A program that exists on your system, but not in your PATH directories won’t be found.

Show the PATH

echo $PATH

# Output:
# /Users/brendan/.composer/vendor/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

The order is important. The program found earliest in the list of paths is executed, and further directories are not checked.

  1. /Users/brendan/.composer/vendor/bin
  2. /usr/local/bin
  3. /usr/bin
  4. /bin
  5. /usr/sbin
  6. /sbin

Extend the PATH

Edit your shell profile to extend the PATH permanently:

nano ~/.zshrc

Add one of these to your shell profile:

# Look in `/custom/directory` before looking in PATH
export PATH="/custom/directory:$PATH"

# Look in `/custom/directory` after looking in PATH
export PATH="$PATH:/custom/directory"

Remember to source your profile, so the changes take effect.

See Workspace > Mac Apps > ZSH for documentation on GravDept’s customized shell profile.

Reference