Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Variables, quoting and the environment

A variable gives a value a name, so you type it once and use it everywhere. How you quote it decides what a command receives, and where you set it decides which programs can see it.

Naming a value

Nobody types a friend's phone number from memory every time they call. You save it once under a name and use the name from then on. The shell offers the same convenience. A variable is a name that holds a piece of text, and you create one by writing the name, an equals sign and the value:

greeting=hello
echo $greeting

The second line prints hello. A $ in front of a name asks the shell to replace the name with its value before the command runs. This replacement is called expansion, and it matters more than it first seems: echo never sees the word greeting, only hello. You have already met expansion with $HOME and $PWD in Finding your way around files and directories; those are variables the shell sets for you.

A few rules keep the shell from misreading you. There must be no spaces around the =. If you type greeting = hello, the shell reads greeting as the name of a command and answers greeting: command not found. Names may contain letters, digits and underscores, must not start with a digit, and are case-sensitive, so greeting and GREETING are two different variables. A common habit, followed in this course, is lowercase names for your own short-lived values and uppercase names for settings that other programs read.

Expanding a name that was never set is not an error. It simply becomes empty, which makes typing mistakes easy to miss. Braces help when a name runs straight into other text. Suppose you want a file called report_2026.txt:

$ file=report
$ echo $file_2026.txt
.txt
$ echo ${file}_2026.txt
report_2026.txt

In the first echo, the shell looked for a variable called file_2026, because underscores and digits can be part of a name. It found nothing and printed only .txt. The braces in ${file} mark exactly where the name ends.

A variable lasts until you remove it with unset greeting, or until you close the terminal. Each terminal window runs its own shell, with its own variables, so a value you set in one window does not appear in another.

Quoting and word splitting

Values with spaces are where variables start to surprise people. Try to store a city name:

$ city=Cape Town
Town: command not found

The shell split the line at the space, as it always does. It took city=Cape as a variable setting for a command named Town, then tried to run that command. Quotes keep the value together:

city='Cape Town'

The shell has two kinds of quotes, and the difference is whether expansion still happens inside them. Single quotes keep everything exactly as written: no variables, no special characters. Double quotes keep spaces together but still expand $. When you need a literal dollar sign inside double quotes, put a backslash in front of it.

You typeThe shell prints
echo 'Hello $city'Hello $city
echo "Hello $city"Hello Cape Town
echo "Tickets cost \$5"Tickets cost $5
echo "It's in $city"It's in Cape Town

The last row shows another use for double quotes: a single quote cannot appear inside single quotes, but it can sit inside double quotes without any trouble.

Quoting matters just as much when you use a variable as when you set it. After the shell expands an unquoted $city, it splits the result at spaces into separate arguments. This is called word splitting. printf '[%s]\n' from Terminals and shells shows it, because it prints each argument it receives in its own brackets:

$ printf '[%s]\n' $city
[Cape]
[Town]
$ printf '[%s]\n' "$city"
[Cape Town]

Unquoted, the command received two arguments. Quoted, it received one. The difference becomes serious with commands that change things. Suppose file='my notes.txt'. Then rm $file asks rm to delete two files, one called my and one called notes.txt, and leaves the file you meant alone. An unquoted value that contains * or ? is also expanded into matching file names, just like the patterns in Finding your way around files and directories, which can be just as surprising.

The habit that avoids all of this is short: put double quotes around every variable you use, as in "$file", unless you specifically want it split. The rest of this course follows that habit in every example.

Capturing a command's output

A variable can also hold what a command prints. Wrap the command in $( and ), and the shell runs it and puts its output in that spot. This is called command substitution:

$ today=$(date +%Y-%m-%d)
$ echo "Report for $today"
Report for 2026-10-05

The argument +%Y-%m-%d tells date how to format its answer: %Y is the four-digit year, %m the month and %d the day. The shell removes the newline at the end of the output, so the value fits neatly inside other text.

Command substitution works inside double quotes, just like a variable, and it does not need a variable at all:

echo "You are $(whoami), with $(ls | wc -l) entries in $PWD"

Inside single quotes it does nothing, for the same reason $city does nothing there. The result of a substitution is split into words like an unquoted variable, so the same habit applies: keep it inside double quotes. You may also meet an older form that uses backticks, such as `date`. It does the same job, but $( ) is easier to read and to nest, so prefer it.

Defaults and taking values apart

A value you need may be missing, or hold more than you need. Bash handles both while it expands the variable, without running another program.

${name:-guest} gives the value of name, or guest when name is empty or was never set. It saves a separate check for a missing value, and it makes a missing value visible instead of silently empty: echo "City: ${city:-not set}" prints City: not set rather than a bare City:.

Other forms cut text from either end of the value. Take file=photo.2026.jpg:

You typeThe shell givesWhat happened
${file%.*}photo.2026Removed the shortest match of .* from the end
${file%%.*}photoRemoved the longest match from the end
${file#*.}2026.jpgRemoved the shortest match of *. from the start
${file##*.}jpgRemoved the longest match from the start

% cuts from the end and # from the start, and doubling either one cuts as much as the pattern allows. The patterns use * for "any characters", as file name patterns do. A fixed piece of text works too: with url=https://photos.example.com, ${url#https://} gives photos.example.com.

The environment, export and PATH

Every program that runs is a process, and the shell is one too. When the shell starts a program, that program becomes its child process. A new bash started from your shell is a child like any other, called a child shell, and it starts with none of your shell's own variables.

What a child does receive is a set of named values called its environment. Programs use it to learn about their surroundings: where your home directory is, which language you prefer, where to look for other programs. printenv prints one value by name, and env lists them all:

$ printenv HOME
/home/ava

Values in the environment are called environment variables, and by convention their names are uppercase: HOME, USER, SHELL, LANG and PATH are some you will see. The variables you create yourself are shell variables: they live inside the shell and are not passed to the programs it starts. export copies a variable into the environment. You can see the difference with bash -c, which runs the text that follows it in a child shell and then returns:

$ color=blue
$ bash -c 'echo "child sees: $color"'
child sees:
$ export color
$ bash -c 'echo "child sees: $color"'
child sees: blue

The single quotes keep your shell from expanding $color itself, so the child does the expansion and shows what it received. Before export, it received nothing.

Two details make this easier to reason about. First, the copy goes one way: a child gets its own copy of the environment, and anything it changes disappears when it ends. Second, for a command you type yourself, such as echo "$color", the shell expands the variable before the command runs, so export makes no difference there. Export matters for programs that read a setting from the environment on their own, such as a child shell running a file of commands with bash, which can only expand the variables it received.

You can create and export in one step with export NAME=value, or set a variable for a single command by writing it in front. Many programs read TZ to choose a time zone, so TZ=UTC date shows the time in UTC, the zone servers and logs use, without changing TZ for anything else.

The most important environment variable is PATH. When you type a command name, the shell does not search your whole computer. It looks through the directories listed in PATH, in order, and runs the first program with that name:

$ echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The directories are separated by colons. This is why the order of installed copies mattered in Setting up your terminal: a directory earlier in PATH wins. To add your own directory, put it in front of the existing value, keeping the rest: export PATH="$HOME/bin:$PATH". If you ever replace PATH by mistake, ordinary commands start answering command not found. Opening a new terminal gives you a fresh shell with the usual value.

Settings files and source

Settings often live in files of NAME=value lines. Ava keeps the details of a trip in one, written with a here-document from Connecting commands with pipes and redirection:

cat > trip.env <<'EOF'
TRIP_CITY='Cape Town'
TRIP_DAYS=4
EOF

There are two ways to run those lines, and they end differently. bash trip.env starts a child shell, which sets the variables and then exits, taking them with it. Your shell never sees them. source trip.env runs the lines in your current shell instead, so the variables stay:

$ bash trip.env
$ echo "${TRIP_CITY:-not set}"
not set
$ source trip.env
$ echo "$TRIP_CITY for $TRIP_DAYS days"
Cape Town for 4 days

Sourcing a file sets ordinary shell variables, so a program you start afterwards still sees them only if they are exported.

color=blue

This shell sees it
Yes
A child shell sees it
No
Survives after the child exits
Yes

export color=blue

This shell sees it
Yes
A child shell sees it
Yes, a copy
Survives after the child exits
Yes

bash trip.env

This shell sees it
No
A child shell sees it
Only the child that ran it
Survives after the child exits
No

TRIP_CITY is gone

source trip.env

This shell sees it
Yes
A child shell sees it
Only if exported
Survives after the child exits
Yes

TRIP_CITY stays in this shell

A child gets a copy of the environment. Nothing it changes comes back.

Where a value is set decides which shells can see it, and whether it is still there when a child shell exits.

Bash sources a file itself when it starts: an interactive bash runs a file in your home directory called .bashrc. A line you add there, such as the PATH change above, applies to every new bash you start. On macOS, the setup lesson added exactly that kind of line, to put Homebrew's OpenSSL ahead of the copy built into the system. Because source runs every line as if you had typed it, source only files you wrote yourself or have read and understood. A downloaded file can look like a list of settings and still contain commands, so later labs read downloaded values as text instead of sourcing the file.

Optional detail: login shells and .bash_profile

The first shell of a session, called a login shell, reads .profile or .bash_profile instead of .bashrc. Ubuntu's own .profile loads .bashrc, and on macOS the bash you start by typing bash is not a login shell, so in this course .bashrc is always read. If you later make bash your login shell on macOS, add the line source ~/.bashrc to ~/.bash_profile so it loads your settings too.

Keeping secrets out of sight

Many command-line tools need a secret, such as a token that tells an API who is calling it. Whoever presents the token is treated as that caller, so it needs the same care as a password. The obvious way to hand it over is the one to avoid:

export API_TOKEN=s3cr3t-value-do-not-do-this

The token is now on your screen, where a screen share, a screenshot or someone behind you can see it. It is also in your shell's history. Bash remembers the command lines you type, so the Up arrow can bring them back, and saves them to a file called .bash_history in your home directory. The history command lists them, numbered, and anyone who can read that file later can read the token too.

The read command avoids both problems. It waits for one line from the keyboard and stores it in a variable. With -p it first prints a prompt, so you know it is waiting:

$ read -r -p 'Which city? ' city
Which city? Cape Town
$ echo "$city"
Cape Town

The whole line you type becomes the value, spaces included, so no quotes are needed. read takes whatever line arrives next, so run it on its own: in a pasted block of several lines, the line after it would become the answer. The -r option keeps any backslashes exactly as typed instead of treating them as special characters; use it every time. For a secret, add -s, which makes read silent, so nothing appears while you paste, not even dots:

read -rs API_TOKEN && export API_TOKEN

Run the line, paste the token and press Enter. &&, from Reading and changing files, exports the variable only if read succeeded. History records the command line, read -rs API_TOKEN && export API_TOKEN, and never the text you pasted, because that text was input to read, not part of a command. This is the line every lab uses for your lab key.

Since you cannot see the value, check it without printing it, by counting its characters:

$ printf '%s' "$API_TOKEN" | wc -c
40

printf '%s' prints the value exactly, without adding a newline, and wc -c counts the bytes it receives. A count that matches the expected length tells you the whole token arrived, and a 0 tells you nothing did. On macOS, wc pads the number with spaces, which you can ignore.

Bash has one more way to keep a line out of history: start it with a space. This works only when the HISTCONTROL variable includes ignorespace, and the line still appears on your screen, so read -rs remains the better choice for secrets.

Ubuntu 24.04 (Windows with WSL, or Linux)

Ubuntu's default .bashrc already sets HISTCONTROL=ignoreboth, which includes ignorespace and also skips a line that repeats the one before it. Check it with echo "$HISTCONTROL".

macOS 14 or later with Homebrew

Homebrew's bash starts with HISTCONTROL unset, so a leading space does nothing until you set it. Add it to your .bashrc so every new bash has it:

echo 'HISTCONTROL=ignoreboth' >> ~/.bashrc
source ~/.bashrc

The screen and history are not the only places a secret can show. When you hand one to another program as an argument, as in some-tool --token "$API_TOKEN", the shell expands it first, and while that program runs, other people signed in to the same computer can list its arguments with ps, a command that lists running processes. Commands built into the shell, such as read, echo and printf, run inside it as no separate process and never appear there. Sending requests with curl shows how to give curl a token without making it an argument.

When you no longer need a secret, remove it with unset API_TOKEN, or close the terminal. Keep secrets out of .bashrc too, because that file is plain text and is loaded into every shell you open. And if a secret ever does end up on screen or in history, treat it as exposed: replace it with a new one from whoever issued it. Deleting the history line does not remove copies that someone may already have seen.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

You set file='my notes.txt' and then run rm $file without quotes. What does rm try to do? 1 of 3

Choose one answer.

You load a token with read -rs API_TOKEN, then run bash check.sh, a file of commands that uses $API_TOKEN. Inside it the value is empty. What is missing? 2 of 3

Choose one answer.

trip.env holds the line TRIP_CITY='Cape Town'. You run bash trip.env, and then echo "$TRIP_CITY" prints an empty line. What keeps the value in your shell? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity