Environment variable, referencing another environment variable - windows

I found, that Rapid Environment Editor program, displays some variables as of type "expandable string". Such variables can refer another variables, for example
JAVA_HOME_45 = ...
JAVA_HOME = %JAVA_HOME_45%
PATH = %JAVA_HOME%\bin
The problem is that such variables are not working from time to time.
The question is: at which level are they implemented? Is this Windows feature? If yes, then how to activate/deactivate/debug it?

I just tested it and it works (like JimHawkins said) with the standard environment variables editor.
This means that it is a Windows feature and it should always be turned on.
You can "debug" it if you open a Console and enter: echo %PATH%.

Related

System variables aren't evaluated in Windows command prompt, user variables are

I would like to be able to cd into a folder that is added to a system as a system variable in Windows 11. Here is a visual representation of the problem:
As you can see, windows command prompt is able to "resolve" variables from the "User variables" section of the Environment variables configuration in Windows, however variables from the "System variables" aren't.
I need to cd into %VS140COMNTOOLS% folder but as you can see it won't be resolved by the command prompt. Is this by design?
VS140COMNTOOLS also doesn't appear in set output.
How can I navigate into %VS140COMNTOOLS%?
System variables and user vars end up in a single environment block. When expanding env vars, there's no concept of user/system. User vars override system vars.
Also: each process gets its own copy of env vars which is resolved when the process launches. Therefore, if a process appears to be "missing" an env var, one of the following happened:
The variable was set after the process launched. For example maybe this command prompt was open during the installation of something that created an env var. You'll need to re-launch the command prompt to get the var.
OR, the process modified it / removed it.

OSX 10.9 Mavericks environment variables: how to set environment variables

How I can EXPORT environment variable not just for one tab but for all system?
If i will use export JAVA_HOME=/Library/Java/Home it will set JAVA_HOME only for current terminal tab and after system restart I will need do it one more time.
How I can set environment variable globally to make by default?
How I can edit variables in $ env list?
Add an entry to ~/.bash_profile:
export JAVA_HOME=/Library/Java/Home
save it, (or create it if it doesn't exist)
quit Terminal.app
re-launch and you're in business.
This is the best place to add the entry in my opinion, although for the distinct differences on OS X of where to add environment variables specifically for one reason or another see:
https://apple.stackexchange.com/a/13019
And for a more generalized UNIX overview:
What's the difference between .bashrc, .bash_profile, and .environment?
You can set environment variables by adding the export commands to various configuration files. E.g. ~/.profile
You can find out more about what files can be used to modify your bash environment by reading the bash manual (man bash). If you're using a different shell then it's probably similar and its man page should contain the same info. You can also read this answer on unix.stackexchange.com, which has some of these details.
If you want to set environment variables for your entire user environment, including GUI applications, I believe you can do that using ~/.MacOSX/environment.plist.

Environment variable behavior in Windows

I have a question regarding environment variables in Windows. So, I set an environment variable using SETX command:
SETX my_var VAR
After that, I use SET m to see if my variable is set. It shows it isn't. However, the variable appears in HKCU\Environment. I exit the command prompt window and open a new one. Typing SET m again makes my variable appear.
I restart explorer.exe and open a new command prompt window. When using SET m, my variable isn't there anymore, but it is in HKCU\Environment.
I don't understand this behavior. Shouldn't it appear also after restarting explorer.exe, giving the fact that it is still in HKCU\Environment? I am missing something, I just don't understand what.
Later edit: I have tried doing the same thing on a computer running Windows 7 and I found out there is no problem. Don't know what is wrong in my case. I want anyway to thank everybody for their help.
Works for me on windows 8.1
What windows version are you on?
setx sets the variable for future sessions, not any existing session (including this one).
No-one ever claimed that there was any logical reason for the 'not this one' quirk. It's eacy to overcome though - just add SETXX.BAT somewhere on your path
SETXX.BAT
SETX %1 %2
set "%1=%2"
and execute it as
call SETXX my_var VAR
(you may need to play around with quotes if you are attempting to set(x) a value containing spaces)

%M2% not working in system variable Path but in user variable Path only

I am installing Maven and need to set M2 and M2_Home. I have set them in the user variables in Environment variables. And I added ;%M2% to the Path variable in the System variables in Environment variables. I was expecting to be able to run mvn --version but I couldn't. However if I do
echo %Path% I can see that there is %M2% in the Path and if I echo %M2% I can see the bin directory that mvn is in.
So I had this problem. The problem did not get solved until I created a Path variable in user variables and added %M2% to that one (and removed it from the path in System variables). Now it works. Does any one know why it is only working in this specific way?
You'd have to look at the Windows source code to be certain what's going on, but this doesn't surprise me. Based on my testing, it appears that system environment variables can only reference other system environment variables, not user environment variables.
Note that echo %PATH% should show the expanded path. You shouldn't see %M2%.
This is probably because system environment variables sometimes need to be expanded in situations where there is no user context. If user environment variables were included in the expansion of system environment variables this would need to be treated as a special case. It's also possible that this was considered the preferred behaviour so that a user's environment variables couldn't unexpectedly affect the interpretation of system variables, or that there are issues related to the way environment variables are inherited between processes.
Either add %M2% to the user path as you've done, or make M2 a system rather than a user environment variable.

Environment variables not updating during deployment

What we're doing:
We're doing an automated deployment using a tool called Nolio. One of the steps we need to do is to set a few environment variables for applications that are being deployed - for example, JAVA_HOME pointing to our preferred java install directory.
We're using the SET command to permanently set the environment variables - and in most ways, it works great. If I right click on my computer and go into environment variables, they all appear perfectly.
The problem:
Unfortunately, later in the deployment, some command line commands are executed that rely on the environment variables, and the environment variables appear to not be set. Using SET without parameters verifies this by displaying all currently available variables.
Now, if I restart the computer, the command line commands work fine. So, the issue is that while the variables are permanently set and do appear in the GUI, they are not propagated to the command prompts until I reboot.
Another interesting tidbit: If I put the commands in a BAT file and double click it, it runs fine, but if I execute it in the command prompt the variables don't resolve prior to a reboot.
Does anyone know a way around this?
First, what version of Nolio do you use?
The Environment variables to which you set value, in the context of one Nolio action, stay in the scope of this action. (It's like opening two different shells on every action)
The best practice for this case would be using the environment variables arrays inputs in the Nolio 'Run Command Line' action. You should write two arrays of parallel Env variable names and values, and give them as input to the 'Run Command Line' action.
It appears your variables are not in scope for the command prompt. At what point in your deployment process are you using the SET command? Interesting that the GUI recognizes the values, but the command prompt doesn't until you've restarted.
Also, I'm not clear as to why using a .bat file is undesired. I can come up with my own reasons, but what are yours?
EDIT
I've found this article that shows a step that you didn't mention. Have you tried:
rem Set the JAVA_HOME environment variable and insert it into the system path.
rem This will make the javac and java commands reachable from the command line.
set JAVA_HOME="C:\Program Files\Java\jdk1.5.0_14"
set PATH=%JAVA_HOME%\bin;%PATH%
I'm not entirely sure why the command prompt won't recognise commands and the batch files will, but you could use SETX as an alternative to SET to see if that resolves your issues.

Resources