Linux List All Environment Variables Command

How do I display all my environment variables using bash shell on a RHEL / Debian / Ubuntu / CentOS / Fedora / Mint Linux operating systems? Can you tell me Linux command to list all shell environment variables?

You can use any one of the following command to display and list the shell environment variables and their values. The printenv command list the values of the specified environment VARIABLE(s). If no VARIABLE is given, print name and value pairs for them all.
  1. printenv command – Print all or part of environment.
  2. env command – Display all exported environment or run a program in a modified environment.
  3. set command – List the name and value of each shell variable.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unix OS
Est. reading time 1m

Linux list all environment variables command

I recommend that you use the printenv command. The syntax is:

printenv
printenv | less
printenv | more
Fig.01: Command to see a list of all currently defined environment variables in a Linux bash terminal

Fig.01: Command to see a list of all currently defined environment variables in a Linux bash terminal

A list of the commonly used variables in Linux

We use the printf command/echo command to display values of the shell varible in Linux.

System Variable Meaning To view variable value type
BASH_VERSION Holds the version of this instance of bash. echo $BASH_VERSION
HOSTNAME The name of the your computer. echo $HOSTNAME
CDPATH The search path for the cd command. echo $CDPATH
HISTFILE The name of the file in which command history is saved. echo $HISTFILE
HISTFILESIZE The maximum number of lines contained in the history file. echo $HISTFILESIZE
HISTSIZE The number of commands to remember in the command history. The default value is 500. echo $HISTSIZE
HOME The home directory of the current user. echo $HOME
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read builtin command. The default value is <space><tab><newline>. echo $IFS
LANG Used to determine the locale category for any category not specifically selected with a variable starting with LC_. echo $LANG
PATH The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. echo $PATH
PS1 Your prompt settings. echo $PS1
TMOUT The default timeout for the read builtin command. Also in an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the command. If not input provided it will logou user. echo $TMOUT
TERM Your login terminal type. echo $TERM
export TERM=vt100
SHELL Set path to login shell. echo $SHELL
DISPLAY Set X display name echo $DISPLAY
export DISPLAY=:0.1
EDITOR Set name of default text editor. export EDITOR=/usr/bin/vim

set and env command

You can use the env / set command too:

env
env | more
set 
set | more

Sample outputs:

HOME=/home/vivek
vivek@nas01:~$ env
TERM=xterm-256color
SHELL=/bin/bash
XDG_SESSION_COOKIE=9ee90112ba2cb349f07bfe2f00002e46-1381581541.324726-906214463
SSH_CLIENT=192.168.1.6 60190 22
SSH_TTY=/dev/pts/1
USER=vivek
MAIL=/var/mail/vivek
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
PWD=/home/vivek
LANG=en_IN
SHLVL=1
HOME=/home/vivek
LANGUAGE=en_IN:en
LOGNAME=vivek
SSH_CONNECTION=192.168.1.6 60190 192.168.1.10 22
_=/usr/bin/env

A note about env/set command

The env will only display a list of environment variables that have been exported and it will not show all bash variables. The set command allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables. If no options or arguments are supplied, set displays the names and values of all shell variables and functions, sorted according to the current locale, in a format that may be reused as input for setting or resetting the currently-set variables. Hence, I recommend that you use printenv command to dump the list of all shell variables on screen. To save the list of all shell environment variables to a file, enter:

printenv > env.txt
cat env.txt

Use the grep command to search for particular variable:

printenv | grep foo
printenv | grep HOME

Conclusion

You learned about listing all Linux shell environment variables. See the following resources for more information:


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • stellar_geisha Jul 19, 2015 @ 19:57

    Thank you <3

  • aref ghobadi Aug 18, 2015 @ 8:33

    thank you :)

  • Harish Feb 21, 2016 @ 18:03

    Thank you for this useful information.

  • Akshata N Jun 7, 2016 @ 12:03

    Thank you.It’s really helpful.

  • 13E Dec 12, 2017 @ 3:34

    Thank you. This is a very informative.

  • Peter Evans Oct 8, 2020 @ 9:47

    Note that set will show all unexported shell variables (e.g. like PS1, TMOUT) whereas env or printenv will not.

  • André Nov 21, 2020 @ 12:42

    With environment variables, what’s the maximum amount of characters a environment variable can have?
    and is it possible to set this higher?

    • 🐧 Vivek Gite Nov 22, 2020 @ 5:40

      The theoretical max length of an environment variable is around 32,760 characters. The maximum size of an environment variable value depends upon execve(). Try:
      false | xargs --show-limits
      Which gives out:

      Your environment variables take up 3310 bytes
      POSIX upper limit on argument length (this system): 2091794
      POSIX smallest allowable upper limit on argument length (all systems): 4096
      Maximum length of command we could actually use: 2088484
      Size of command buffer we are actually using: 131072
      Maximum parallelism (--max-procs must be no greater): 2147483647
      

      On my box it went up to 512MiB and failed with the following message (see image here)[1]:

      bash: xrealloc: cannot allocate 18446744071562068096 bytes


      Another command:
      getconf -a |grep MAX
      See the following man page:
      man 2 execve

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum