Print all environment variables, including hidden ones

2010-11-09 2 min read bash Fedora Linux

Print all environment variables, including hidden ones

  <td>
    <div class="text codecolorer">
      for _a in {A..Z} {a..z};do _z=${!${_a}*};for _i in `eval echo "${_z}"`;do echo -e "$_i: ${!_i}";done;done|cat -Tsv
    </div>
  </td>
</tr>
1

This uses some tricks I found while reading the bash man page to enumerate and display all the current environment variables, including those not listed by the ‘env‘ command which according to the bash docs are more for internal use by BASH. The main trick is the way bash will list all environment variable names when performing expansion on ${!A*}. Then the eval builtin makes it work in a loop.

I created a function for this and use it instead of env. (by aliasing env).

This is the function that given any parameters lists the variables that start with it. So ‘aae B’ would list all env variables starting wit B. And ‘aae {A..Z} {a..z}’ would list all variables starting with any letter of the alphabet. And ‘aae TERM’ would list all variables starting with TERM.

  <td>
    <div class="text codecolorer">
      aae(){ local __a __i __z;for __a in "$@";do __z=${!${__a}*};for __i in `eval echo "${__z}"`;do echo -e "$__i: ${!__i}";done;done; }
    </div>
  </td>
</tr>
1

And my printenv replacement is:

  <td>
    <div class="text codecolorer">
      alias env='aae {A..Z} {a..z} "_"|sort|cat -v 2>&1 | sed "s/\^\[/\ ?33/g"'
    </div>
  </td>
</tr>
1

From: http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

* View this command to comment, vote or add to favourites * View all commands by AskApache

commandlinefu.com

by David Winterbottom (codeinthehole.com)

URL: http://www.commandlinefu.com/commands/view/6899/print-all-environment-variables-including-hidden-ones

Enhanced by Zemanta
comments powered by Disqus