Linux Basics: Using Environment Variables
Environment variables pass named values to programs. On Debian and Ubuntu, Bash also has shell variables, which stay in the current shell unless you export them.
Read existing values
printf '%s\n' "$HOME"
printf '%s\n' "$PATH"HOME normally contains your home directory. PATH is a colon-separated list of directories the shell searches when an external command name contains no slash.
The dollar sign reads a variable’s value. Double quotes keep that value together even when it contains spaces. With printf, %s prints the value and \n adds a line break.
Create and export a variable
Use the same Bash terminal for this sequence:
BLOG_PRACTICE_TOPIC='Linux basics'
printf '%s\n' "$BLOG_PRACTICE_TOPIC"
export BLOG_PRACTICE_TOPIC
printenv BLOG_PRACTICE_TOPICThere are no spaces around = in an assignment. The first line creates a shell variable. export makes it available to programs started from that shell, such as printenv.
A program inherits a copy of the environment when it starts. Exporting a variable does not update another terminal that is already open.
Set a value for one command
BLOG_PRACTICE_TOPIC='Archives' printenv BLOG_PRACTICE_TOPIC
printf '%s\n' "$BLOG_PRACTICE_TOPIC"
unset BLOG_PRACTICE_TOPICArchives
Linux basicsThe assignment before printenv supplies a different value for that command only. The following printf still reads the earlier value. unset removes the practice variable from your shell.
These examples do not edit startup files. They affect only the current shell and programs it launches, so they are a simple way to practice without changing future terminal sessions.
Reference
Acknowledgements
This post was created with assistance from AI.
Linux Basics — lesson 11 of 25. Series index
Previous: Comparing Files with diff and cmp · Next: Using Bash Exit Status