← Back to course

Environment Variables, Aliases, and Shell Basics

Environment Variables, Aliases, and Shell Basics

Welcome back.

In the previous lesson, you learned how to pack and unpack files with archives.

Now we enter the world of the shell.

This is where the terminal starts remembering things.

Variables. Paths. Aliases. Configuration files.

Basically, we teach the terminal a few habits.

Hopefully good ones.

What You’ll Learn

In this lesson, you’ll learn how to:

The Mission

Your mission is simple:

Learn how the shell stores information and how to create shortcuts for commands you use often.

Because typing long commands again and again is not discipline.

It is suffering with extra steps.

Print Text with echo

The echo command prints text.

Try:

echo "Hello, terminal!"

You should see:

Hello, terminal!

Simple.

Useful.

Very polite.

echo is often used to print values, test variables, or create small text files.

What Is a Variable?

A variable is a name that stores a value.

Create one:

name="Viktor"

Now print it:

echo $name

You should see:

Viktor

Important: do not put spaces around =.

This is correct:

name="Viktor"

This is wrong:

name = "Viktor"

The shell sees spaces and gets confused.

Very powerful.

Very sensitive.

Like a genius cat.

Built-in Environment Variables

Linux already has many environment variables.

Try:

echo $HOME

You may see:

/home/viktor

HOME stores the path to your home directory.

Try:

echo $USER

You may see your username.

Try:

echo $SHELL

You may see something like:

/bin/bash

or:

/usr/bin/zsh

That shows which shell you are using.

The PATH Variable

One of the most important variables is PATH.

Try:

echo $PATH

You may see something like:

/usr/local/bin:/usr/bin:/bin:/home/viktor/.local/bin

PATH tells the shell where to search for commands.

When you type:

ls

the shell looks through the directories in PATH until it finds the ls program.

Without PATH, you would have to type full command locations like a very tired robot.

Find Where a Command Lives

Use:

command -v ls

You may see:

/usr/bin/ls

Try:

command -v bash
command -v zsh
command -v python
command -v node

Some may exist.

Some may not.

The terminal is honest.

Sometimes brutally.

Temporary Variables

Variables created in the shell are usually temporary.

Try:

course="Linux Terminal"
echo $course

You should see:

Linux Terminal

Now close the terminal and open it again.

Try:

echo $course

It will probably be empty.

The shell forgot it.

Not because it is rude.

Because you did not save it.

Export a Variable

To make a variable available to child processes, use export.

Example:

export COURSE_NAME="Linux Terminal"

Now run:

echo $COURSE_NAME

You should see:

Linux Terminal

Environment variables are often used for configuration.

For example:

export API_URL="http://localhost:8080"
export NODE_ENV="development"

Developers use this all the time.

Because hardcoding secrets into code is how projects become crime scenes.

Show Environment Variables

To see many environment variables, use:

env

This may print a lot of output.

You can search inside it:

env | grep HOME

Or:

env | grep PATH

The | sends output from env into grep.

This is called a pipe.

Pipes are one of the reasons the terminal is powerful.

And also one of the reasons people start smiling strangely while using Linux.

What Is an Alias?

An alias is a shortcut for a command.

Example:

alias ll="ls -la"

Now run:

ll

It works like:

ls -la

This is useful because some commands are long and life is short.

Useful Aliases

Here are some useful examples:

alias ll="ls -la"
alias gs="git status"
alias c="clear"
alias ..="cd .."

Now:

..

works like:

cd ..

This feels like cheating.

But it is allowed.

Linux is not against shortcuts.

Linux is against nonsense.

Mostly.

Check Existing Aliases

To see existing aliases:

alias

You may already have some.

Many systems define aliases for safety or convenience.

For example:

alias rm="rm -i"

This asks before deleting.

Very careful.

Very parental.

Remove an Alias

To remove an alias:

unalias ll

Now try:

ll

It may no longer work.

That is normal.

You removed the shortcut.

The terminal does not hold grudges.

Usually.

Save Aliases Permanently

Aliases created in the terminal disappear when you close it.

To save them, add them to your shell configuration file.

If you use Bash:

nano ~/.bashrc

If you use Zsh:

nano ~/.zshrc

Add:

alias ll="ls -la"
alias c="clear"
alias ..="cd .."

Save the file.

Then reload it.

For Bash:

source ~/.bashrc

For Zsh:

source ~/.zshrc

Now your aliases stay.

The terminal has learned.

A proud moment.

Almost emotional.

Check Which Shell You Use

Run:

echo $SHELL

If you see Bash:

/bin/bash

use .bashrc.

If you see Zsh:

/usr/bin/zsh

use .zshrc.

If you are using Arch Linux with Zsh, yes, you are probably exactly the kind of person who will enjoy aliases too much.

Common Mistakes

Spaces around =

Wrong:

name = "Viktor"

Correct:

name="Viktor"

The shell is strict here.

No spaces.

Not negotiable.

Forgetting $

This prints the word name:

echo name

This prints the variable value:

echo $name

The $ means:

Give me the value stored inside this variable.

Without $, the shell thinks you just like saying names.

Saving aliases in the wrong file

If you use Zsh and edit .bashrc, nothing may happen.

If you use Bash and edit .zshrc, nothing may happen.

Check your shell first:

echo $SHELL

Then edit the right file.

Measure twice.

Edit once.

Practice

Try this:

echo "Shell practice"
echo $HOME
echo $USER
echo $SHELL
echo $PATH
course="Linux Terminal"
echo $course
alias ll="ls -la"
ll
unalias ll

Then answer:

  1. What does echo do?
  2. What does $HOME show?
  3. What does $PATH do?
  4. What does alias create?
  5. Why do aliases disappear after closing the terminal?

Mini Challenge

Create three aliases:

alias ll="ls -la"
alias home="cd ~"
alias ports="ss -tuln"

Then:

  1. Run ll.
  2. Run home.
  3. Run ports.
  4. Save these aliases in your .bashrc or .zshrc.
  5. Reload the config file with source.

No mouse.

The mouse is now watching from retirement.

Summary

Today you learned:

The shell is not just a place where you type commands.

It is an environment you can customize.

Carefully.

With power.

And hopefully without turning your terminal into a circus.

Next Lesson

In the next lesson, we’ll write our first Bash script.

That is where commands stop being individual actions and start becoming automation.

The terminal is about to do your boring work for you.

Finally.