Setting Our Own Variables

Learn how to set up our own shell variables

Environment variables

We can set our own environment or shell variables in addition to the ones set up for us. This is helpful when we have to store long strings like API keys, access tokens, or credentials. Many web frameworks and applications use environment variables to store database credentials and other sensitive information to keep it out of the code.

Creating a new variable

Let’s try it out. We create a new shell variable named SHIELD_PASSWORD and assign it the value of 12345, and then we print out the value:

$ SHIELD_PASSWORD=12345
$ echo $SHIELD_PASSWORD

This variable is available at the shell level, so it’s not available to any subshells or any other programs. We use the env command to verify this and use grep to filter for SHIELD_PASSWORD:

$ env | grep SHIELD_PASSWORD

Use the terminal below to practice these commands.

Get hands-on with 1200+ tech skills courses.