When working in a Linux terminal, your Bash shell relies on a massive dictionary of hidden variables to know how to behave. These variables dictate everything from the colors of your text prompt and the location of your home directory, to the paths where the system searches for executable programs. If a script fails to run or a program behaves unexpectedly, the issue is often a misconfigured variable. To troubleshoot these issues, you need to see exactly what variables are currently active in your session. This is where the set command comes in.
Using the set Command to View Variables
The set command is a built-in shell utility. When executed without any arguments, it dumps the entire list of currently defined shell variables, environment variables, and active shell functions directly to standard output.
set
Because modern Linux systems define hundreds of variables and massive blocks of bash completion functions by default, running this command will usually result in a massive wall of text scrolling instantly past your screen. To make it readable, you should pipe the output into a pager like less:
set | less
You can now use your arrow keys or Page Up/Page Down to scroll through the list at your own pace. Press q to quit the pager when you are finished.
Finding a Specific Variable
If you are looking for a specific variable (for example, checking your PATH variable to see where the system looks for commands), scrolling manually is inefficient. Instead, pipe the output of set directly into grep to search for a keyword.
set | grep PATH
This will instantly filter the massive list and only print the lines containing the word “PATH,” allowing you to quickly verify that your custom directory was successfully added to the environment.
The Difference Between set, env, and printenv
New Linux users often confuse set with the env or printenv commands, as they appear to do similar things. The critical difference lies in scope:
envandprintenv: These commands only display exported environment variables. These are global variables that are passed down to child processes and external scripts.set: This command displays everything. It shows the global environment variables, but it also shows local shell variables (variables you defined in the current terminal window but did not export) and all defined shell functions. It is the most comprehensive view of your current shell’s state.