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: [donotprint][/donotprint]
Sh, Ksh, or Bash shell user type the set command.
Csh or Tcsh user type the printenv command.
How the environment variables defined
The syntax is as follows on a bash/sh/ksh:
var="this is a test"# you must use quotations when you have white spacever=4.18.5
var="this is a test" # you must use quotations when you have white space
If you are using tcsh/csh shell, try:
setvar="this is a test"setver=4.18.5
set var="this is a test"
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:
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:
Tip: Print value of an individual shell variable
To print value of HOME variable use echo command or printf command as follows:
printf "%sn" "$HOME"
printf "Hi, %s! You are using %s shelln" "$USER" "$SHELL"
Sample outputs from bash shell on OS X Unix based system:
Hi, vivek! You are using /bin/bash shell
See bash(1) shell Linux/Unix man page for more information.
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.