Shell Variables

Operating System vs. Shell

Shell Variables

WINDOW="neighbor"
echo "outer: window is ${WINDOW}"
bash src/shell_var_inner.sh
echo "inner: window is ${window}"
outer: window is neighbor
inner: window is 

Exercise: Single Quotes

What happens if you modify the scripts shown above to use single quotes instead of double quotes?

Environment Variables

WINDOW="neighbor"
export THRESHOLD=0.5
echo "outer: window is ${WINDOW} and threshold is ${THRESHOLD}"
bash src/env_var_inner.sh
echo "inner: window is ${window} and threshold is ${threshold}"
outer: window is neighbor and threshold is 0.5
inner: window is  and threshold is 

Exercise: Setting in Children

If a child process sets shell or environment variables, are they visible in the parent once the child finishes executing?

Environment Variables in Programs

WINDOW="neighbor"
export THRESHOLD=0.5
echo "outer: window is ${WINDOW} and threshold is ${THRESHOLD}"
python src/env_var_py.py
import os

window = os.getenv("WINDOW", default="not set")
threshold = os.getenv("THRESHOLD", default="not set")
print(f"inner: window is {window} and threshold is {threshold}")
outer: window is neighbor and threshold is 0.5
inner: window is not set and threshold is 0.5

Inspecting Variables

env | cut -d = -f 1 | sort | head -n 10
BASH_SILENCE_DEPRECATION_WARNING
CONDA_DEFAULT_ENV
CONDA_EXE
CONDA_PREFIX
CONDA_PREFIX_1
CONDA_PROMPT_MODIFIER
CONDA_PYTHON_EXE
CONDA_SHLVL
EDITOR
GEM_HOME

Exercise: Python vs. the Shell

The os.environ variable in Python's os module is an easy way to get all of the process's environment variables. Compare it to what env shows.

  1. Are there differences?

  2. If so, what are they and why do they exist?

Important Environment Variables

Name Typical Value Purpose
EDITOR nano default text editor
HOME /Users/tut user's home directory
LANG en_CA.UTF-8 user's preferred (human) language
PATH see below search path for programs
PWD /Users/tut/sys present working directory
SHELL /bin/bash user's default shell
TERM xterm-256color type of terminal
TMPDIR /var/tmp storage for temporary files
USER tut current user's name

Search Path

echo $PATH | tr : '\n'
/Users/tut/google-cloud-sdk/bin
/Users/tut/conda/envs/sys/bin
/Users/tut/conda/condabin
/Users/tut/.nvm/versions/node/v20.8.0/bin
/Users/tut/bin
/Applications/Postgres.app/Contents/Versions/14/bin
/Users/tut/go/bin
/usr/local/bin
/System/Cryptexes/App/usr/bin
/usr/bin
/bin
/usr/sbin
/sbin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
/Library/Apple/usr/bin
/Library/TeX/texbin
/usr/local/bin

Adding to the Search Path

export PATH="/tmp/bin:${PATH}"
echo $PATH | tr : '\n' | head -n 5
/tmp/bin
/Users/gregwilson/google-cloud-sdk/bin
/Users/gregwilson/conda/envs/sys/bin
/Users/gregwilson/conda/condabin
/Users/gregwilson/.gem/ruby/3.1.2/bin

Exercise: Shortening the Path

Removing a directory from PATH is harder than adding one. Write a shell script that:

  1. Splits PATH on colons to put one entry on each line.
  2. Uses grep to remove the undesired line.
  3. Uses paste -s -d : to recombine the lines.
  4. Uses command interpolation to assign the result back to PATH.

This exercise may remind you why complicated operations should be done in Python rather than in the shell.

Startup Files

source $HOME/.bashrc

Command Interpolation

wc -l $(ls src/*.text)
      12 src/ctrl_z_background.text
      12 src/kill_int.text
       6 src/kill_process.text
      30 total