Environment variable behavior in Windows - 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)

Related

Can't escape the space when changing Shell in nvim

I'm trying to change the nvim shell to Powershell 7, i've tried adding set shell=powershell and it works, the problem is that doing that way comes with a massive slowdown on every action what i perform in nvim (saving, using nvim-tree, even starting up nvim). Reading a bit online found someone who solved this issue by manually adding the shell path instead of the set shell=powershell mentioned before, behind the idea that the powershell being used was the 32-bit one instead of the 64.
So, i've tried to do the same and added this.
set shell=C:/Program\ Files/PowerShell/7/pwsh.exe
But that didn't work, every time i've tried to open the terminal with :term i got
E475: Invalid value for argument cmd: 'C:/Program' is not executable
Any ideas?
Thanks in advance!

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

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.

Setting an environment variable in Cygwin

I have been trying to setup a environment variable in Cygwin using the command export PRIMOSBASE=/directory/for/primosfiles.
And when i check the variable using the command echo $PRIMOSBASE it shows the /directory/for/primosfiles. hopeful this means the environment variable is set.
But when i try to run a shell script(primos) for the /directory/for/primosfiles, it shows
./primos: line 8: /prilaunch.pl: No such file or directory
chmod: failed to get attributes of `step1.sh': No such file or directory
which means i have not set the PRIMOSBASE environment. could anyone please tell me where i am going wrong...
Thanks ...
Run
echo "export PRIMOSBASE=/directory/for/primosfiles" >> ~/.bashrc
to append the command to the end of your .bashrc file, so that the variable is set each time you use Cygwin. Then run
source ~/.bashrc
to make it take effect immediately.
NOTE: Make sure you use double brackets (>>) to append. It might be a good idea to make a backup of .bashrc just in case. If you're not comfortable with I/O redirection, an alternative is to edit .bashrc with an editor. I think vim is among the default tools in Cygwin.
I had a similar issue trying to get ANDROID_HOME to work in a Cygwin window. When I used the linux path separators, as follows
ANDROID_HOME=/cygdrive/c/Users/User/AppData/Local/Android/sdk my gradlew build script complained it couldn't find the sdk in ANDROID_HOME.
I eventually discovered that I had to set my environment variable in the Windows format, including Windows path separators '\', as follows
ANDROID_HOME=C:\Users\User\AppData\Local\Android\sdk
Note: the PATH and several other environment variables set in Windows are converted into Linux format. I hope this helps others who want/need to use Cygwin + Windows + essentially Windows programs that need environment variables.

Reading cmd.exe variables inside a MinGW Makefile

I am writing installation in a Makefile in which I need to set the PATH env. variable.
In the windows part of it, I found the following:
set: With set PATH="%PATH%;%CD%" I can change the PATH inside the running environment. There are two problems with this:
The environment is a spawned cmd.exe by make which gets its variable affected and the effect removed as soon as it closes
Even if the previous problem could be solved, still the cmd.exe that calls make would close one day and the modified PATH lost.
setx: A microsoft tool that can permanently change env. variables. According to microsoft itself, this is the only command-line option to do this. Using setx PATH "%PATH%;%CD%" -m however, turns path into the literal %PATH%;%CD% and doesn't replace the variables by their contents!
Note that I am calling make from cmd.exe not cygwin or other modified windows shells that act more like linux. What I'm saying is that, although I can use $(PATH) in my makefile (instead of %PATH%), I can't use pwd (instead of %CD%)
Also note that if in cmd itself I run:
setx PATH "%PATH%;%CD%" -m
it works perfectly. Somehow I need to make make execute this command.
Do you have any idea how to fix this, or what workaround do I have?
P.S. Just for the record, echo "%PATH%;%CD%" in the Makefile also echoes the literal "%PATH%;%CD%" rather than let cmd.exe handle it
Back in the day i Borland C++ Free Command Line tools included a version of make which played well with the dos/windows command line. Probably still floating around somewhere.
Workaround:
Create a .bat file, put the command there, and invoke it from the Makefile.
I still am interested in a direct fix in the Makefile though.

Resources