Print all environment variables, including hidden ones

Print all environment variables, including hidden ones

1
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

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.

1
aae(){ local __a __i __z;for __a in "$@";do __z=${!${__a}*};for __i in `eval echo "${__z}"`;do echo -e "$__i: ${!__i}";done;done; }

And my printenv replacement is:

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

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

One thought on “Print all environment variables, including hidden ones”

Leave a Reply