List all variables with set command
In the previous steps, you learned how to display individual variables using echo
and printenv
. But what if you want to see all the variables that are currently set in your shell environment?
The set
command, when used without any arguments, will list all shell variables, including environment variables and shell-specific variables.
Type the following command in your terminal and press Enter:
set
You will see a long list of variables and their values. This output includes environment variables (like HOME
, USER
, PATH
) as well as variables specific to your current shell session.
...
HOME=/home/labex
HOSTNAME=...
HOSTTYPE=x86_64
...
LANG=C.UTF-8
...
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
...
SHELL=/usr/bin/zsh
...
USER=labex
...
The output can be quite extensive. You can scroll through it to see the different variables that are set by default in your environment.
Comparing the output of set
and printenv
(if you were to run printenv
without arguments), you would notice that set
includes more variables. This is because set
shows all shell variables, while printenv
only shows environment variables.
Understanding the difference between shell variables and environment variables is important as you delve deeper into Linux. Environment variables are inherited by child processes, while shell variables are typically local to the current shell session.
You have now learned three ways to interact with variables in Linux: echo
(for displaying individual variables), printenv
(for displaying environment variables), and set
(for listing all shell variables).
Click Continue to complete this lab.