Latticework

Command Palette

Search for a command to run...

Bash / Shell

Scripting

14 min

Explanation

A shell variable is just a name bound to text: NAME=value sets it (no spaces around =), and $NAME (or ${NAME}) substitutes its value anywhere in a later command — including inside another string. export marks a variable so it's available beyond the current line the same way a plain assignment is here. Two built-in variables are always available: $HOME (your home directory) and $PWD (your current directory, updated automatically by cd). Wrapping a variable in single quotes turns off expansion — '$NAME' prints the literal four characters $NAME, not its value; double quotes still expand normally.

NAME=forge
echo hello $NAME
# hello forge
mkdir $NAME
touch $NAME/README.md
Try it

export and a plain NAME=value assignment behave the same way in this simulator — there's no subshell boundary here for the distinction to matter. $HOME always resolves to /home/user, the directory every session starts in.

Loading editor…
Exercise

Set a variable `PROJECT` to `forge`, then create a directory named after it and create an empty file `README.md` inside that directory, using the variable both times (not the literal word `forge`).

Quiz

What's the difference between `echo $NAME` and `echo '$NAME'` (single quotes)?

Checkpoint

You can set and use shell variables, and you understand how quoting affects expansion.