How to View Environment Variables in Linux: Complete Command Guide
Understand environment variables in Linux
Environment variables serve as dynamic values that affect run processes and programs in Linux systems. These variables store crucial information like system paths, user preferences, and configuration settings that applications rely on to function right. Understand how to view and manage these variables is fundamental for system administration, troubleshooting, and software development.
Linux environment variables exist in two primary scopes: global variables that affect all users and process system-wide, and local variables that entirely impact the current user session or specific processes. The system mechanically set many environment variables during startup, while users and applications can create custom variables as need.
Basic commands for view environment variables
Use the env command
The
Env
Command provide the near straightforward method for display all environment variables in your current session. Plainly open a terminal and type:
Env
This command output a comprehensive list of all environment variables and their current values. The output display each variable in the format
Variable_name = value
, make it easy to identify specific settings and their assign values.
For better readability, pipe the output through sort:
Env | sort
This alphabetically organize the variables, make it easier to locate specific entries in longer lists.
The print env command
The
Print env
Command offer similar functionality to
Env
But with additional flexibility. Use it without arguments to display all environment variables:
Print env
The real power of
Print env
Emerge when view specific variables. Alternatively of scroll through hundreds of entries, target individual variables:
Print env path
This command display merely the path variable’s value, which contain the directories where the system search for executable files.
Use the set command
The
Set
Command displays both environment variables and shell variables, provide a more comprehensive view of your session’s variable state:
Set
Since
Set
Typically, produce extensive output, combine it with filtering tools:
Set | grew variable_name
This approach help locate specific variables within the larger output set.
Advanced techniques for variable inspection
Filtering and searching variables
Real world scenarios oftentimes require find variables that match specific patterns or contain particular strings. The
Grew
Command excels at this task:
Env | grew i path
This command find all variables contain” path ” rrespective of case sensitivity. The
I
Flag ensure the search capture variations like path, path, or path.
For more complex pattern matching, use regular expressions:
Env | grew' ^[a z ] * _'
This pattern match variables that start with uppercase letters follow by an underscore, help identify system define variables that follow common naming conventions.
View variables with echo
The
Echo
Command provide a quick method for display specific variable values. Prefix the variable name with a dollar sign:
Echo $ home
This display your home directory path. The echo method work specially advantageously in scripts and when you need to verify a single variable’s value quick.
For variables that might not exist, use parameter expansion with default values:
Echo $ { custom_var: "variable not set" }
This technique display the variable’s value if it exists, or the default message if it don’t.
Common environment variables and their purposes
System path variables
The path variable rank among the virtually critical environment variables in Linux systems. It contains a colon separate list of directories where the shell search for executable commands:
Echo $ path
Understand path help troubleshoot command execution issues and explain why some programs work from certain locations but not others.
The LD_library_path variable serve a similar function for shared libraries, tell the system where to find dynamic libraries that programs need to run.
User and session variables
Several variables store information about the current user and session:
-
User
contain the current username -
Home
points to the user’s home directory -
Shell
specifies the default shell program -
Pwd
show the current work directory
These variables help programs locate user specific files and understand the current session context.
Display and terminal variables
Graphics and terminal relate variables control how applications interact with the display system:

Source: screenrant.com
-
Display
specifies the x11 display server for graphical applications -
Term
defines the terminal type for proper text format -
Lang
set the system language and locale
Practical applications and use cases
Troubleshooting system issues
Environment variables oftentimes hold the key to resolve system problems. When applications fail to start or behave circumstantially, examine relevant variables can reveal configuration issues.
For example, if a program can not find required libraries, check the LD_library_path:
Print envLDd_library_path
If this variable is empty or miss necessary paths, it explains the program’s failure to locate dependencies.
Development and programming
Software developers often need to verify environment variables that affect build processes, runtime behavior, and application configuration. Many development tools rely on specific variables:
Print env java_home
Print env python_path
Print env node_env
These variables control how development environments and applications behave, make their verification essential for consistent development workflows.
System administration tasks
System administrators use environment variable inspection to understand system configuration, verify security settings, and ensure proper application deployment. Variables like sudo_user, ssh_client, and log name provide valuable information about user sessions and security contexts.
Work with variable output
Formatting and processing output
Raw environment variable output can be overwhelming. Several techniques help format and process this information more efficaciously:
Env | column t ' ='
This command create a two column table with variable names and values decently align, improve readability importantly.
For variables with multiple values separate by colons (like path ) use
TR
To display each entry on a separate line:
Echo $ path | try':' n'
This technique make it easier to examine each directory in the path severally.

Source: etsy.com
Saving variable information
Sometimes you need to save environment variable information for later analysis or comparison. Redirect output to files:
Env > current_env_vars.txt
This creates a snapshot of all current environment variables that you can examine previous or compare with other system states.
Security considerations
Environment variables sometimes contain sensitive information like passwords, API keys, or security tokens. Exercise caution when display or share environment variable output, specially in share environments or when create documentation.
Use filtering to exclude sensitive variables from output:
Env | grew v ' ( (ssword|secret|token|key )')
This command display environment variables while exclude those likely to contain sensitive information base on common naming patterns.
Shell specific considerations
Different shells may handle environment variables somewhat otherwise. While the commands discuss work across most shells, some variations exist:
In bash, the
Declare
Command provide additional variable information:
Declare p
This show variables along with their attributes, such as whether they’re export to child processes.
In ash, similar functionality exist with additional features for variable manipulation and display.
Best practices for variable management
When work with environment variables, follow these best practices:
Document important custom variables and their purposes to help future troubleshooting efforts. Use descriptive names that clear indicate the variable’s function and scope.
Regularly audit environment variables to identify obsolete or conflicting settings that might cause system issues. Remove unused variables to maintain a clean environment.
Test variable changes in isolated environments before apply them system all-inclusive, specially for critical system variables that affect multiple applications.
Understand how to view environment variables in Linux empower users to troubleshoot issues, configure applications decent, and maintain system health efficaciously. These techniques form the foundation for more advanced system administration and development tasks, make them essential skills for anyone work with Linux systems.