Unix/ Linux: Print Environment Variables Command

Posted on in Categories , , , last updated February 4, 2014

I am a new shell user. How do I print or list environment variables on Linux and Unix-like operating system using shell prompt? In Linix and Unix, how can I print values of all my environment variables?

As per your shell name (version) use the following command to display and print your environment variables at the shell prompt:

  1. Sh, Ksh, or Bash shell user type set command.
  2. Csh or Tcsh user type printenv command.

Print environment variables on sh/ksh/bash Linux & Unix shell

Open the Terminal and type the following command:
$ set
OR
$ set | more
OR
$ set | grep 'USER'
Sample outputs:

Fig. 01: Bash/SH/KSH: set Command Display Environment Variables and Functions
Fig. 01: Bash/SH/KSH: set Command Display Environment Variables and Functions

Print environment variables on csh/tcsh Linux & Unix shell

Open the Terminal and type the following command:
$ printenv
OR
$ printenv | more
OR
$ printenv | grep 'USER'
Sample outputs:

Fig.01: CSH/TCSH shell: printenv Command Display Environment Variables
Fig.01: CSH/TCSH shell: printenv Command Display Environment Variables

Tip: Print value of an individual shell variable

To print value of HOME variable use echo command or printf command as follows:

echo "$HOME"

OR

printf "%s\n" "$HOME"

OR

printf "Hi, %s! You are using %s shell\n" "$USER" "$SHELL"

Sample outputs from bash shell on OS X Unix based system:

/Users/vivek
Hi, vivek! You are using /bin/bash shell

See bash(1) for more information.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

1 comment

Leave a Comment