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

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.

Related

fastlane: Ruby environment variable ENV['PWD'] return nil/empty path

In the ruby script for fastlane, I am trying to access an environment variable ENV['PWD'], but it returns as empty path/nil.
When I execute command env from mac terminal, it shows PWD=/abcd/project
If you want to get the process's current directory, use Dir.pwd.
If you want to get the value of the environment variable, use ENV['PWD'].
You can use ps awwxue command to inspect the environment of running processes to figure out why your process doesn't have PWD set.
One way of clearing the environment is to use env -, e.g. env - env.
After Quitting terminal and launching terminal again, it starts working!

Can I set an environment variable on Bash's command line?

I am trying to set an environment variable for Bash. However, I need this to be set before any of the shell's startup scripts (including /etc/profile), because /etc/profile acts differently based on the value of this variable.
Specifically, I want to create a shortcut to MinTTy that works like git-bash, but I need to set the MSYSTEM environment variable before the shell starts, or at least before it starts processing any startup scripts.
A solution that has MinTTy setting the environment variable before it starts the shell will also be accepted.
Edit:
What I am really looking for is sort of a command-line option to BASH that will set an environment variable, somewhat akin to the -D option to most C (and other) compilers. This would be a "general case" solution. Alternatively, a similar option (command line or configuration) to MinTTy will also do the job.
For my specific need, I have an idea for a potential work-around: Run a BASH script - with no startup scripts - that sets my required variable and execs another shell as a login shell.
Define the target of your shortcut file as follows:
C:\cygwin64\bin\mintty.exe /bin/bash -l -c "MSYSTEM=MINGW64 exec -l bash"
This command:
invokes bash directly as a login shell (-l)
passes it a command (-c) that defines the environment variable of interest (MSYSTEM=MINGW64) and then invokes a new copy of bash (exec -l bash), which inherits the existing environment, plus the new definition, but sources the profile(s) again, due to -l
(and prepends - to the executable name reported in $0 (-bash), as would happen if you started Mintty with just -, which is what the regular Cygwin64 Terminal shortcut does).
An alternative is to set the environment variable in Windows first.
[Not an option for the OP] If the environment variable should always have the same value, set it persistently as follows: run sysdm.cpl, go to the Advanced tab, click on Environment Variables... and define variable MSYSTEM as needed.
To define the variable ad-hoc, create a batch file as follows and make the shortcut target that batch file:
#echo off
# Define the env. variable with the desired value.
set "MSYSTEM=MINGW64"
# Invoke Mintty with a login shell, which will now see the env. variable.
# Adjust the path to mintty.exe as needed.
c:\cygwin64\bin\mintty.exe -
Note: Opening the batch file from a shortcut briefly opens a regular console window before opening Mintty, which may be undesired.
A simple helper WSH script, as demonstrated in this answer of mine, can prevent this.
You should just be able to do the same as you do in command prompt. Therefore, you can do:
set VAR=VarContents
Although I already accepted an answer above, I found this link that specifically addresses the second part of my question (Mintty specific) or an alternative way of setting an environment variable before running a command.
The contents of the Windows shortcut can be:
C:\cygwin64\bin\mintty.exe -t "Title" /bin/env "MSYSTEM=MINGW64" /bin/bash -l
(Suggested by Mintty Tips:Setting environment variables.)

Environment variable, referencing another environment variable

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%.

%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