Open CheatSheet Back To List Next Tutorial

Environment Variables and Aliases (Video Tutorial)

Environment Variables and Aliases

Intro

[0:00 Video timestamp]

This is the fourth in a series of tutorials on how to use Unix/Linux for software developers.

This section covers environment variables. What they are. How to access them. How to create them. It also covers shell startup files, which is where you add your own env variables. And covers aliases which allow you to add your own shortcut commands.

It is recommended to open your system's shell program and follow along with the topics covered.

Read it, watch it, do it, review it:
  • There is both a written and accompanying video version of this tutorial. There are timestamps under the headings that align with the video version of the topic.
    • Read it: For each topic heading, First read the topic. 
    • Watch it: Then watch the associated section of the video.
    • Do it: Then follow the instructions to replicate the steps in your Unix Shell program.
  • The Unix CheatSheet has a major category that aligns with this tutorial.
    • Review it: When you are done with the whole tutorial, open the CheatSheet and review the Environment Variables and Aliases category from this tutorial. Make sure you understand everything, and can refer back to the CheatSheet when you are working on your own projects in the future. 



Startup Files

[0:22 Video timestamp]

Environment variables are part of all Unix-like operating systems and Windows.
Running programs can access the values of environment variables for configuration purposes.

Shell startup files
[1:05 Video timestamp]

Add your environment variables to one of your shell's startup files. Those are files that start when the shell is launched.

There are different opinions on which startup file you should use, so we'll cover the most commonly sited ones.

  • Bash: If you are using the Bash shell program, add your environment variables to the .bash_profile or .bashrc file.
  • Z shell: If you are using Z shell, add them to .zshenv or .zshrc.
In case you are curious, rc stands for run command.

These files go in your home directory. The shell looks for them there when it launches. 

To see if you have them, run the list command in your Home directory with the -A option to include hidden files.
ls -A ~/ 

If you are on a Mac, you can also look in the Finder app.
The file names start with a dot so they are hidden. 
In finder, to show or hide the hidden files press Cmd+Shift+.

If you are on Mac you should have a file named .zshrc.

If you don't have any of those files then add one. For Z Shell, add: touch ~/.zshrc

Open the startup file in a text editor. 



Variables

[0:35 Video timestamp]

Variables: are name-value pairs stored in memory. The variable name is an identifier used to reference the value.

From the command line you can print variables with the echo command. Preface the variable name with the dollar sign.
echo $VAR_NAME Returns the value.



Built in Shell Variables

Your shell program has some built-in variables including but not limited to:

SHELL: Path to the Shell's executable file (e.g., /bin/bash)
echo $SHELL Would return /bin/bash if you use Bash, or bin/zsh if you use Z shell.


HOME: Path to the user's home directory. Equivalent to echo ~.
echo $HOME Returns /Users/my-username

USER: The current username.
echo $USER Returns your username


PWD: The present working directory's absolute path.
echo $PWD



Local Variables

You can create and access local variables in the Unix shell. They are only stored while the shell session is open.

Use the declare keyword followed by the name value pair.
declare VAR_NAME=value 

The Syntax is as follows:
  • Variable name: Can contain letters, numbers and underscores, but can only start with a letter or underscore.
  • Case: Uppercase name is not required but is the convention.
  • Quotes are optional unless the value has a space in it. Can be single or double quotes. Single quotes are literal, while double quotes allow interpolation of other variables.
  • Retrieve Variables by prefacing them with a dollar sign: $VAR_NAME

As an example, let's set a greeting variable: 
declare GREETING=Hello

Print the variable value:
echo $GREETING Prints "Hello"



Environment Variables

[2:49 Video timestamp]

Environment variables have a global scope. You put them in the startup file and export them so they are available to your programs. 

Let's add an environment variable named EMAIL.

Note: Web developers often use environment variables for sensitive information to keep it private. Especially when collaborating on a project using a remote repository like GitHub. So email, user ids, passwords and things like that can be stored in an environment variable on your computer and also on the web server.

Put it in the start up file and save it.
.zshrs | .zshenv | .bash_profile | .bashrc
export EMAIL="steve@example.com"

Preface the variable with the export keyword to export it from this file to the Unix shell environment.

Environment variables are set when the shell starts, so to access the EMAIL variable you need to either
  • Close and restart the Terminal, 
  • Or execute the source command with an argument containing the path to the startup file: source ~/.zshrc

Then enter the echo command to print the variable:
echo $EMAIL Returns steve@example.com

env command
[4:11 Video timestamp]
To see all the environment variables run the env command:
env
It will return the email variable we added, plus the built-in variables.


Customize the Shell prompt
You may want to customize your shell program's prompt. You do that by setting the PS1 environment variable. 
To see the default value, run: 
echo $PS1


To customize the Shell prompt, you can set the PS1 environmental variable.
To set it to show the name of the current directory followed by a $, padded with a space, set the PS1 variable as follows. 
export PS1="%1~ \$ "

%1~ Displays the current directory name.
 \$  Displays a $ at the end of the prompt followed by a space. 



Path

[5:04 Video timestamp]

Now let's take a look at Path.
PATH is An environment variable containing a set of paths to your executable files. 
To execute a program from the command line you can either:
  • Enter the absolute path to the executable file.
  • Or enter just the program name. Then the system will look in all the paths in the Path variable for that file name.

To see the path variable enter echo $PATH
The paths are in a single string separated by colons.

The system paths on a Mac are: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
So the system executable files are stored in one of the blow directories:
  • /usr/local/bin
  • /usr/bin
  • /bin
  • /usr/sbin
  • /sbin 

To see which executable files are in those directories, either go to Finder, or from the command line use the ls command: ls /usr/local/bin

If you install a program that puts it's executables in a different directory than one of the above, it needs to be added to the path. 

I installed a Postgres database that has it's executable files in /Library/PostgreSQL/14/bin.

To add that to the path, add a PATH variable in your startup file:
export PATH="$PATH:/Library/PostgreSQL/14/bin"

$PATH holds the built-in PATH variable containing the system paths. 
Follow it with a colon then the new Postgres path. 

Now see the new path value: echo $PATH

To see where a particular program's executable is stored run:
which program Returns the path to the program's executable file.

which zsh Returns the path to the executable file for the Z shell program: /bin/zsh
which ls Returns the path to the Unix list program: /usr/bin/ls
which less Less is the Unix Paging program. It returns /usr/bin/less
which node If you installed Node.js, this would return: /usr/local/bin/node
which postgres After adding Postgres to the path, this would return /Library/PostgreSQL/14/bin/postgres



Aliases

[8:04 Video timestamp]

The last topic we'll cover in this section is aliases.
An alias is a command shortcut. 
You use them to shorten frequently used commands or to correct incorrect input.
You put them in your startup folder.

The format to add an alias is alias alias_name="the command in quotes"

For example you can change the list command to always show hidden files by including the -A option.

.zshrs | .zshenv | .bash_profile | .bashrc
alias ls="ls -A"

To have the alias take effect in our shell immediately without having to close and reopen the shell use the source command on the startup file.
source ~/.zshrc

Now if you run the ls command it will return all files including the hidden ones.

Another useful alias: if there is a project you cd into frequently, you can add an alias for it. Adjust the name and path to the project's name and path

alias cdmyproj = "cd ~/path/to/myproj"

Then typing in cdmyproj will take you to that project's directory. 

Open CheatSheet Back To List Next Tutorial