Declaring global variable works as it suppose to do.
But i fails when you try to declare it indirectly using a variable.
Just wanted to chime in on this one.
First, as others already described - you need to declare the variable again (global) in order for your script to "read" it.
If you have a nested function that also uses that variable, you have to re-declare it again inside the nested function as well. (Even if it's declared outside and above it)
The problem begins with the usage of the "execute" command. Execute runs the supplied command in a sub-shell - so you get no interaction with it whatsoever.
Allow me to take it step by step as to what happens here:
:local i 1; <--- local variable "i" is declared with a value of 1, being local means it can only be read by the region it's been put into (script, curly braces, etc).
:execute ":global TrafWAN$i"; <--- $i is resolved into value 1, so the string that's getting executed is ":global TrafWAN1". But this command was ran in a sub-shell, once it completes - POOF! Everything's gone! ..think of it more like calling another script, from your script, to do stuff for you. You cannot interact with it, it only executes whatever is enclosed in its quotes.
:global type [:typeof $TrafWAN1]; <--- the command "typeof" attempts to fetch the type of $TrafWAN1 which at this point hasn't been declared in the script and is therefore "unknown".
The reason this works on the terminal and not on the script, is that on the terminal you can "read" the global variables without having to declare them prior.
Plus, on the terminal version of your code, you can even skip the "execute" line altogether because it offers virtually nothing to you. (it just executes a global declaration and exits)