Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Finding all the ENV variables in Linux

Posted on 2012-08-18
Medium Priority
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

omarfarid earned 400 total points
ID: 38308053
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 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38308111
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 85

Assisted Solution

ozo earned 600 total points
ID: 38308320
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

farzanj earned 600 total points
ID: 38308592
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

ID: 38308689
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 !!

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses

564 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