LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   understand variable type in shell script (https://www.linuxquestions.org/questions/programming-9/understand-variable-type-in-shell-script-481564/)

qrshat 09-08-2006 06:44 AM

understand variable type in shell script
 
hi guru,

in shell script i have got a variable and i want to search the type of this variable. i don't know the value is numeric or string and i am looking for a utility,function or command that return the type of the variable.

For example A=123 or A=abc. and i want to search that does A store a numeric value or sting value?

thanks in advance.

jlliagre 09-08-2006 06:51 AM

With ksh or bash, "typeset -p" will show the variable type, if any.

qrshat 09-08-2006 07:40 AM

Quote:

Originally Posted by jlliagre
With ksh or bash, "typeset -p" will show the variable type, if any.

thanks jlliagre

but "typeset -p" shows the variables and their values in my solaris.
but my main problem is the type of the variable. is it numeric or string. i just want to learn the variable is numeric or string. i don't interest its value.

jlliagre 09-08-2006 07:59 AM

Nope, Solaris (I assume you really mean ksh on Solaris) shows the type of the variable, should it have a type. eg:
Code:

$ typeset -p | grep RANDOM
typeset -i RANDOM

Here RANDOM is of type integer, but the shell, being loosely typed, doesn't mandates a variable to have a type, in which case it is a string by default.

AnanthaP 09-08-2006 09:52 AM

You could try:
A_Num=expr {$A+"0"}
if [ A_Num -eq A ] and so on ..

End

qrshat 09-09-2006 02:12 AM

Quote:

Originally Posted by AnanthaP
You could try:
A_Num=expr {$A+"0"}
if [ A_Num -eq A ] and so on ..

End

Thanks,
it's a nice idea. :) and it sounds good..


All times are GMT -5. The time now is 09:34 AM.