Variables, Quoting and Aliases
Create shell and environment variables, understand single versus double quotes, capture command output, and make settings permanent in .bashrc.
Welcome back!
Step 1 of 14Learn
The shell remembers things
Every time you open a terminal, the shell sets up dozens of variables, named values it and other programs can read. You have already met one: PATH. A few more: HOME (your home directory), USER (your user name), PWD (the current directory) and SHELL (the path to your shell).
Read a variable by putting $ in front of its name:
$ echo $HOME
/home/lena
$ echo "Logged in as $USER on $HOSTNAME"
Logged in as lena on webserverCreate your own with name=value. No spaces around the equals sign: backup_dir=/srv/backups works, while backup_dir = /srv/backups makes the shell try to run a command called backup_dir. This is the single most common scripting error beginners make.
A plain variable exists only in the current shell. To pass it to programs you start from that shell, export it, which turns it into an environment variable:
$ export EDITOR=nano
$ env | grep EDITOR
EDITOR=nanoNow git, crontab and other tools that check EDITOR will open nano. env or printenv lists all environment variables. By convention, environment variables use UPPER_CASE names and your own script variables use lower_case, which avoids accidentally overwriting system ones like PATH.
Step 2 of 14Compare
Shell variable vs environment variable
| Aspect | Shell variable | Environment variable |
|---|---|---|
| Created with | backup_dir=/srv/backups | export EDITOR=nano |
| Seen by programs you start | No | Yes |
| Naming habit | lower_case | UPPER_CASE |
| List them | set | env or printenv |
Step 3 of 14Practice · Spot the mistake
Which of these lines would fail or do something unexpected in Bash? Tap the two that are wrong.
Step 4 of 14Learn
Quotes change everything
The shell treats some characters specially: spaces split arguments, $ expands variables, * expands file names. Quotes control that.
- Double quotes
"..."keep spaces together but still expand variables and commands.echo "Hello $USER"printsHello lena. - Single quotes
'...'turn off all expansion.echo 'Hello $USER'printsHello $USERliterally. Use them for regular expressions, awk programs and anything containing$you don't want expanded. - A backslash escapes one character:
echo "Price: \$5"printsPrice: $5.
Command substitution runs a command and drops its output into the line: $(command).
$ today=$(date +%F)
$ echo "Backup for $today"
Backup for 2026-03-14
$ tar -czf "backup-$(date +%F).tar.gz" /srv/dataAlways wrap variables in double quotes when you use them as arguments: rm "$file", not rm $file. If file holds My Report.txt, the unquoted version becomes two arguments, My and Report.txt, and tries to delete the wrong things. Quoting variables is the habit that separates fragile scripts from reliable ones.
Step 5 of 14Practice · Choose one
With USER=lena, what does echo 'Logged in as $USER' print?
Step 6 of 14Compare
What quotes do to $USER
| Aspect | No quotes | Double quotes | Single quotes | Backslash |
|---|---|---|---|---|
| You type | echo Hi $USER | echo "Hi $USER" | echo 'Hi $USER' | echo "Hi \$USER" |
| Prints | Hi lena | Hi lena | Hi $USER | Hi $USER |
| Spaces in values | Split into separate words | Kept together | Kept together | Kept together |
Step 7 of 14Learn
Aliases and making settings permanent
An alias is a shortcut for a longer command:
$ alias ll='ls -lah'
$ alias gs='git status'
$ alias rm='rm -i'Now ll runs ls -lah. The rm -i alias is a popular safety net that asks before every deletion. Type alias alone to list current aliases and unalias ll to remove one.
There's a catch: variables, exports and aliases you type disappear when the shell closes. To keep them, put them in a startup file that Bash reads every time it starts:
~/.bashrcruns for every new interactive shell, such as each terminal window. This is where aliases, prompt settings and most exports go.~/.bash_profile(or~/.profile) runs for login shells, such as when you connect over SSH. On many systems it simply loads~/.bashrctoo.- zsh users use
~/.zshrcinstead.
After editing ~/.bashrc, either open a new terminal or run source ~/.bashrc to apply the changes to the current shell.
A typical addition to ~/.bashrc:
export PATH="$PATH:$HOME/bin"
export EDITOR=nano
alias ll='ls -lah'The first line adds your own ~/bin directory to the end of PATH, so scripts you put there can be run by name from anywhere.
Step 8 of 14Practice · Fill in the blanks
Fill in the blanks
To make the alias ll available in every new terminal, add the line alias ll='ls -lah' to the file . To apply the change to the shell you already have open, run .
Step 9 of 14Diagram
Which startup file runs?
Tap a shape with to see more.
- Bash starts
- Login shell (SSH)
- ~/.bash_profile or ~/.profile
On many systems it simply loads ~/.bashrc too.
- New terminal window
- ~/.bashrc
Aliases, prompt settings and most exports go here.
- zsh instead of Bash
- ~/.zshrc
Step 10 of 14Note
Step 11 of 14Note
Step 12 of 14Practice · True or false
If you run export API_URL=https://example.com in one terminal window, the variable is automatically available in all your other open terminal windows.
Step 13 of 14Practice · Match the pairs
Match each syntax to what it does.
Tap an answer, then tap the place where it belongs (or drag it there).
- $HOME
- "$file"
- '$file'
- $(date +%F)
- export VAR=1
Step 14 of 14Practice · Choose one
A script contains rm $target and target is set to old logs. What actually happens when it runs?
Lesson complete!