bash debug – log all executed commands

Screenshot of a Bash 3.1 session demonstrating...
Screenshot of a Bash 3.1 session demonstrating its particularities. Shows exporting a variable, alias, type, Bash’s kill, environment variables PS1, BASH_VERSION and SHELLOPTS, redirecting standard output and standard error and history expansion. A POSIX session is launched from a normal session. Finally, the POSIX session kills itself (since just “exit” would be too boring). (Photo credit: Wikipedia)

Whenever I am writing a script in perl or bash, I always wish that there
was some way to have all the commands logged or output to screen. I know
there is “set -x” option to have debugging enabled, but sometimes that
seems to be too much information and I dont really need all that. So, here
is something I found recently for bash to log all the executed commands.


This will echo/print all the commands on the stdout.


Enhanced by Zemanta

Print all environment variables, including hidden ones

Print all environment variables, including hidden ones

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.

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:

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


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

by David Winterbottom (


Enhanced by Zemanta

Verify all the paths in the PATH directory

Here is the command to test that all the directories in your path actually exist.

(IFS=:;for p in $PATH; do test -d $p || echo $p; done)

And the explanation :

Set the IFS to \”:\”

now we loop through the PATH variable

and test all the directories with \”test -d\”

Here is another version without IFS:

for i in ${PATH//:/ };do test  -d $i || echo $i;done

and next one without the test command:

for i in ${PATH//:/ };do [  -d $i ] || echo $i;done