We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Environment Variables

We talked about how you can create and use local variables in your shell:

name="Lane"
echo $name
# Lane

There is another type of variable called an environment variable. They are available to all programs that you run in your shell.

You can view all of the environment variables that are currently set in your shell with the env command.

Export

To set a variable for your current shell session, use the export command (it won't persist if you close the terminal):

export NAME="Lane"

You can then use the variable in your shell, just as before:

echo $NAME
# Lane

The interesting part is that programs and scripts you run in your shell can also use that variable:

For example, if we have a script called introduce.sh with the following contents:

#!/bin/sh
echo "Hi I'm $NAME"

We can run it and it will use the NAME environment variable we set earlier:

./introduce.sh
# Hi I'm Lane

You can also temporarily set a variable for a single command, instead of exporting it for the whole session.

For example:

WARN_MESSAGE="this works too" bash worldbanc/private/bin/warn.sh

Unset

You can use the unset command to remove an environment variable from your current shell session:

unset NAME

Assignment

Take a look at the contents of the worldbanc/private/bin/warn.sh script. It looks like it's supposed to print nicely formatted warning messages with worldbanc branding...

Submit the shell checks.