Bash variables

From I Will Fear No Evil
Jump to navigation Jump to search

Finding defined variables

Assuming you have done something like foo="bar" at your bash prompt it is not always clear how to echo ALL the defined variables back from your bash session. The following is a good way to get that information when needed. This can also be useful for debugging scripts that you are in the process of building out.

declare -p

If you only want the environment variables, and do not want to use env for some reason

declare -xp

An alternate command that will also give nice and parsable results. This is quite nice, as the loop itself will pull the values that compgen -v gave as just var names.

compgen -v | while read line; do echo $line=${!line};done  

Convert upper to lower case in various ways

Why bother having multiple ways to do the same thing? Merlin software on routers does not have the same tr and sed versions, and are not GNU compiled. However awk in that case does still work with its builtin tolower logic within itself. Always a good idea to know more than one way to do things.

echo FooBar | awk '{print tolower($0)}'
foobar
echo FooBar | tr [:upper:] [:lower:]
foobar
echo FooBar | sed 's/[A-Z]/\L&/g'
foobar

Bash builtins

From reddit of all places

man bash | awk '/   Shell Variables/,/   Arrays/' | more

Bash history

Looks like it can reside in different dot files. For Ubuntu it is in ~/.bashrc

HISTSIZE=2000
HISTFILESIZE=4000