Finding all the ENV variables in Linux

Posted on 2012-08-18
Last Modified: 2012-08-18

My question is, In Linux, how to list ALL the Environment variables (including UNSET variables).  The commands 'set' and 'env' are displaying the ENV variables which are defined.  I'm wondering how to see all the unset ENV variables, which could be useful for tuning certain stuffs in linux machine.

To make my question more clear, here I shown one example where I first try to list all the ENV related to 'history' command. In the first grep command, I can't see an unset ENV variable called "HISTTIMEFORMAT" getting displayed.  Later after I set that variable and export it, I could see it in the output and it altered the History command format. Like this how can I see other unset ENV variables. Please let me know.

[root@linuxmach ~]# set | grep -i hist

[root@linuxmach ~]# export HISTTIMEFORMAT='%F %T '
[root@linuxmach ~]# set | grep -i hist
[root@linuxmach ~]# history
<output truncated>
 1019  2012-08-18 07:00:04 cat environ
 1020  2012-08-18 07:00:34 cat status
 1021  2012-08-18 07:05:54 cd
 1022  2012-08-18 07:06:35 set
 1023  2012-08-18 07:06:47 set | grep -i hist
 1024  2012-08-18 07:07:05 export HISTTIMEFORMAT='%F %T '
 1025  2012-08-18 07:07:09 set | grep -i hist
 1026  2012-08-18 07:07:27 history
Question by:ashsysad
    LVL 40

    Assisted Solution

    If a variable is unset then you can not see it! It would be mentioned in the man page related to the command or tool or shell, etc.
    LVL 37

    Expert Comment

    by:Gerwin Jansen
    What do you want to tune? 'Certain stuffs' I don't understand. Unset variables don't exist and consume no memory, cpu, disk or other resources :-)
    LVL 84

    Assisted Solution

    there are an infinite number of unset variables, as there are an infinite number of uninstalled and unwritten programs

    You can list the HIST variables described in the bash documentation with
    man bash | sed 's/[[:cntrl:]].//g' | egrep -x ' +HIST[[:alpha:]]*'
    LVL 31

    Accepted Solution

    It is really hard to see ALL the environment variables because a lot of these variables are not related to a particular shell -- they may be related to a particular application.  However if you know which shell you are going to use, you can see its variables and if you know which application you are going to use, you can see its variables.  The principle I use is based on the idea that variables do appear in the binaries of a program.  For instance if you want to see the variables of bash you can issue this command:

    strings /bin/bash | grep -P '[A-Z]+'

    Open in new window

    Likewise if some variables are related to, say, vi, you can issue command like
    strings `which vi` | grep -P '[A-Z]+'

    Open in new window

    Please note that there will be a lot of additional stuff and you will have to look for variables carefully.

    Author Comment

    This is awesome !!  While posting this question, I wondered if am asking something stupid. Now with all your answers, I'm convinced that I have asked a sensible question.

    I do found some useful ENV variables such as TIMEFORMAT, HISTCONTROL, HISTIGNORE etc, which I can reset in /etc/bash_profile.  

    The following link seems to contain all the Bash variables explaining each:

    Thanks a lot again !!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now