Search⌘ K

Using Variables

Explore the use of variables in the shell and environment context. Understand how to print variables using printenv and echo, apply variable expansion with the dollar sign, and use curly braces to combine variables with strings accurately. Practice naming files dynamically with user variables and learn why quoting variables helps prevent errors in shell commands.

We'll cover the following...

Printing variables

If we want to print one of the environment variables to the screen, we use the printenv command, followed by the variable name:

$ printenv HOME

Use the terminal below to practice these commands.

Terminal 1
Terminal
Loading...

This is good for checking the value, but we can also use these variables as values in our shell commands. To do so, we prefix the variable with the dollar sign. For example, we print out the HOME variable’s value using the code below:

$ echo $HOME

Use the terminal below to practice these commands.

Terminal 1
Terminal
Loading...

If we omit the dollar sign, we ...