close
close
which command prints partial or full environment variables

which command prints partial or full environment variables

2 min read 24-02-2025
which command prints partial or full environment variables

The command to print environment variables, either partially or fully, depends on your operating system (OS) and the level of detail you need. This article covers the most common methods for Linux/macOS (using Bash) and Windows.

Linux/macOS (Bash)

On Linux and macOS systems using Bash (the most common shell), the primary command for viewing environment variables is printenv.

Printing All Environment Variables

To display all environment variables, simply type printenv and press Enter. This will output a list of each variable and its corresponding value.

printenv

Printing Specific Environment Variables

You can also print specific environment variables using the printenv command followed by the variable name. For instance, to print the value of the HOME environment variable:

printenv HOME

This command only shows the HOME variable and its value. You can list multiple variables by repeating the command for each one.

Using echo with Variable Expansion

Another common method is using the echo command combined with variable expansion. This allows you to access and display the value of a single environment variable.

echo $HOME

Note the $ symbol before the variable name; this is crucial for variable expansion in Bash. This method is more concise for accessing individual variables.

Using env

The env command is very similar to printenv. It prints the current environment variables but can also be used to run a command with a modified environment. For example, to see all environment variables:

env

Windows

On Windows systems, you have a few options to view environment variables:

Using echo

Similar to Bash, the echo command can be used with variable expansion. However, the syntax is slightly different. You need to use %VARIABLE_NAME% instead of $VARIABLE_NAME. For example, to print the value of the USERNAME environment variable:

echo %USERNAME%

Using the set Command

The set command provides a more comprehensive view of environment variables. Typing set in the command prompt will display all environment variables.

set

To see only a specific variable, use the set command followed by the variable name:

set USERNAME

This will show only the USERNAME variable and its value.

System Properties GUI

Windows also offers a graphical user interface (GUI) method to view environment variables. Navigate to System Properties (search for it in the Start Menu), then click on the Advanced tab, and select Environment Variables. This provides a convenient visual representation of your environment variables, categorized by user and system variables.

Choosing the Right Command

The best command depends on your needs:

  • Single variable, quick access: echo (Bash: echo $VARIABLE_NAME; Windows: echo %VARIABLE_NAME%) is the most efficient.
  • All variables: printenv (Linux/macOS) or set (Windows) provides a complete list.
  • Specific variables (multiple): printenv VARIABLE_NAME (Linux/macOS) or set VARIABLE_NAME (Windows) for each variable needed.
  • GUI Access (Windows): System Properties provides a user-friendly visual view.

Understanding these commands gives you full control over accessing and managing your system's environment variables, a critical aspect of system administration and scripting. Remember to always consult your OS documentation for the most accurate and up-to-date information.

Related Posts