Unix / Linux Print Environment Variables Command nixCraft Updated Tutorials/Posts

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 Linux and Unix, how can I print values of all my environment variables?

You can use shell variables to store data and configuration options. You have system and user defined shell variable. One can print them as per shell name/version. This page shows how to read and set environmental and shell variables on a Linux or Unix-like systems.

Unix / Linux Print Environment Variables Command

Use the following command to display and print your environment variables at the shell prompt:

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

How the environment variables defined

The syntax is as follows on a bash/sh/ksh:

var=value
var="this is a test" # you must use quotations when you have white space
ver=4.18.5
dest="/backups"

If you are using tcsh/csh shell, try:

set var=value
set var="this is a test"
set ver=4.18.5
set dest="/backups"

Now you know how to setup the environment for your shell. Let us see how to display the value of environment variable.

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: Unix / Linux Print Environment Variables Command i.e. use 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.02: 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"
echo "$var"

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) shell Linux/Unix man page for more information.

Get 15% off on Linux Foundation certified SysAdmin, Progamming, Kubernetes/Containers and Open Stack certification & course. Use “CYBER15” coupon code.
training.linuxfoundation.org

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Facebook Comments

More Stuff

ZigPress – now with added ZigCare It’s been quite a long time in the making but the new ZigPress website is finally live. We hope you like it. Our goals in this redesign were easy navi...
Linux Kernel /etc/sysctl.conf Security Hardening nixCraft Updated ... How do I set advanced security options of the TCP/IP stack and virtual memory to improve the security and performance of my Linux based system? How d...
Convert files at the command line with Pandoc Pandoc is a command-line tool for converting files from one markup language to another. Markup languages use tags to annotate sections of a document. ...
OERu makes a college education affordable Open, higher education courses are a boon to adults who don’t have the time, money, or confidence to enroll in traditional college courses but want to...
Spread the love

Posted by News Monkey