Gang’s Tech Stuff

Converting lowercase to uppercase in Shell scripts

Posted by: Gangadhar on: March 26, 2009

Have you ever wondered why there is no toUpper() kind of function in shell script? Well, i think the reason is typeset command.

What does it do exactly? A lot:

* Set a parameter as an integer by using -i option so as to fasten the arithmetic operations on that variable.

* Convert to lower case using -l

* Mark a variable as Read-only using -r

etc…

Converting into upper case? Yes, you guessed it right, use -u option.

How to use it? You can use it before assignment or after assignment, does not make much difference.

prompt $> typeset -u temp_var
prompt $> temp_var=hi
prompt $> echo $temp_var
HI
prompt $> temp_var2=hey
prompt $> typeset -u temp_var2
prompt $> echo $temp_var2
HEY

Whats more? You can even give more that one variable to this typeset command.

prompt $> typeset -l temp_var temp_var2
prompt $> echo $temp_var $temp_var2
hi hey

For more information on typeset, use man typeset.

Leave a Reply