Blog is moving

My blog is moving to http://victormendonca.com/blog/. If you are looking for a specific or older post you are in the right place Otherwise check out my new page for more up to date content.

Friday, April 3, 2009

How to display variables without case sensitivity


This is the second of many shell (bash) tips that I'll be posting here. This little script will allow you to display the value of variables without having case sensitiveness or having to enter the “$” before the variable name.

Here's what displaying a variable without the script looks like:

$ echo $PATH
/home/victor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

And here after the script:

$ var path
/home/victor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

$ var PATH
/home/victor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

You can see that both ways will work (as long as the variable does not have the same name in lower and capital case).

And here's the script, which can also be added to ~/.bashrc

var ()
{
_var="$1";
if [ -n "${!_var}" ]; then
echo ${!_var};
else
_var=$(echo "$_var" | tr 'a-z' 'A-Z');
echo ${!_var};
fi
}


No comments: