0

I found some confusing behavior in working with environment variables. If I have two environment variables and one points to another, when I reference the pointer variable, it will be automatically dereferenced and I will get the value of the variable it's pointing to; like in below c_test is equal to foo and when I echo c_test I get foo. b_test points to c_test, and when I echo b_test I get foo. I would expect since a_test is equal to b_test, when echo a_test I should get foo; but I don't. What's happening here? I would expect the dereferencing to chain, but it's not for some reason.

enter image description here

1 Answer 1

1

First of all, this is not MS-DOS. Cmd.exe was loosely based on command.com (the DOS prompt) but it ends there. It is very different but backward compatible (mostly) with the old timey days.

Moreover, the behavior you are seeing has nothing to do with the command prompt. This behavior exists for every process launched by windows.

Why you are seeing the behavior described

One of two reasons (or even both)

Possible reason one:
Windows makes a single pass of the environment variables seen in your list. When windows calls the ExpandEnvironmentStrings() function, _BTest hasn't been resolved yet. In order for what you expect to take place, windows would need to call this function over and over on the whole list until there was nothing left to resolve.

Possible reason two:
Windows uses registry keys to store this information. In order for this functionality to work in the first place, a key must have the REG_EXPAND_SZ type and not the REG_SZ type.

Open the registry editor and look at HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment .. you will see those key types there. You can't just change the type (unfortunatley), you will need to create a new key of the type you want with a dummy name, copy the data, delete (or rename) the original key, rename the dummy key to the name you want.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .