Hone

Lessons · Terminal · variables and export

Variables, and export

NAME=ada sets a variable, with no spaces around the equals sign. $NAME reads it. export NAME hands it to programs started from this shell.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

API keys, ports, paths: programs read their configuration from environment variables, which is exactly what export creates.

How to think about it

Set it, echo it to check, export it if a program needs it, and quote it when you use it in a command.

Worked example

NAME=ada
Set. No spaces around the equals sign.
echo $NAME
ada.
echo "hello $NAME"
hello ada: variables expand inside double quotes.
export NAME
Now child programs can see it.
bash -c 'echo $NAME'
A separate program prints ada.
bash -c 'echo ${CITY:-unknown}'
unknown: a default for a variable that is not set.

Your turn

Make PORT visible to the app you start next.

 PORT=3000

The trap

NAME = ada with spaces. The shell tries to run a command called NAME with two arguments.

Practise variables and export on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.