Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Guys, How can I find out what is 2

Status
Not open for further replies.

bharix

Programmer
Aug 6, 2002
23
DE
Guys,

How can I find out what is the permitable length of
variable names in Korn shell?
Is there any environment setting which can define such
max permitable length of variable name?

e.g
export MY_VAR="my memory var";
here the length of MY_VAR is 6.

Thanks,
bharix

 
I don't recall ever reading that there was a limit on the length for a variable...you should be able to make one as long as you like and I would say that there is not an environmental setting for "max permitable length of a variable name" in ksh either.
 
Whenever I want to find out something like this, I like to ask the computer. Here's a Korn shell script that will try creating variables with ever increasing length for the variable name. It will abort and report as soon as it doesn't create it.
[tt]
#!/bin/ksh

typeset -i COUNT=0

unset NAME

while (( COUNT += 1 ))
do
print "Trying name length: ${COUNT}"

# Create a name for the variable, one char longer each loop
export NAME=${NAME}Z

# Create the variable using that growing name
export ${NAME}=Test

# Uncomment these to see what the values are...
# print "Variable name is: ${NAME}"
# eval print "Value assigned to the big named variable: \$$NAME"
# eval print "Length of value assigned: \${#${NAME}}"

# Test if variable was created and abort if not
eval : \${${NAME}:?Failed on NAME length = ${COUNT}}

# Reset for next loop
unset ${NAME}
done
[/tt]
Basically the variable name is "Z", then "ZZ", then "ZZZ", and so forth. The value set to it is "Test" each time. As soon as the variable doesn't get created, it aborts and tells what length it died on.

I tested this on a Sun Ultra 2 running Solaris 8. It ran to well over 7,500 before I killed it. I don't know what the limit is, but it seemed to have no problem up to this point.

Hope this helps.
 
Thanks for the script and the information.
It cleared my doubts.
Regards,
bharix.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top