Windows environment variables not working in cmd - windows

I just found that i cannot run most commands in windows cmd, such as "git", "java", "node".
I found a lots of answers telling me to set environment variables, the weird thing is, the environment variables looks good to me, and i can actually run these commands in powershell or git bash, but none of them works in windows cmd.
Hope anyone can give me some advise, thank q

As I understand,you are not able to run several commands in windows cmd.
Windows PowerShell is the new Microsoft shell that combines the old cmd functionality with a new scripting/cmdlet instruction set with built-in system administration functionality, so there would be several commands which would work in PowerShell but not in Command Prompt.
Use setx.exe to set persistent environment variables via Command Prompt using following command -
setx [variable_name] "[variable_value]"
Please note, setx.exe does not set the environment variable in the current command prompt, but it will be available in subsequent command prompts.

Related

Why is $OSTYPE returning "linux-gnu" on Windows?

Problem
When I run a bash script on Windows:
bash my_script.sh
I get linux-gnu for $OSTYPE inside the script.
Why is this like this?
I assume that i have WSL installed is relevant here.
Tested in PowerShell and CMD.
Git bash is returning msys like expected! Thx #user1934428
Background
I want to start some python scripts from bash, but not inside WSL.
From my command line I reach different python versions on windows, but from inside the bash scripts it is using the one inside WSL (except for GitBash).
You're right, running the bash command in PowerShell or CMD will launch WSL to run your script. You confirm this (and see which version of WSL) by running cat /etc/issue in your bash script. Your WSL environment will have an independent set of environment variables (not just $OSTYPE). You can see this by comparing the output of Get-ChildItem -Path Env:\ in PowerShell to the output of env (after you launch bash from PowerShell).
I suspect that the python version discrepancy you're seeing is a result of the PATH variable in your WSL runtime not matching what you have set in your PowerShell environment. You can fix your version issue by setting an alias containing a path to the python executable you want to use by adding alias python=/c/path/to/python.exe to the start of your bash scripts.
Alternatively, you can use a tool like Cygwin or git-bash to run your scripts. I'm not sure if they will use the same path variables as Windows so you may need to set those manually too.

"export PYTHONPATH = $PYTHONPATH:." in Windows Command Prompt

This Linux command needs to be rewritten for Windows Command Prompt:
export PYTHONPATH = $PYTHONPATH:.
I have researched quite a bit already.
Please note:
This is not necessarily about PYTHONPATH, but about setting an environement variable from Windows command-line (Command Prompt) in an equivalent manner to the Linux code cited above.

Can not install Windows Service from Bash script [duplicate]

I have a small utility script called clear.bat that does some housekeeping work on my sources.
It is a .bat file so that I could easily double-click it in Windows Explorer.
Sometimes, I find it more handy to execute it from my Git bash (msysgit, if this matters).
To do this, I type
cmd
clear.bat
exit
cmd turns my Git bash into a normal cmd window where I could easily execute my batch. When I type exit, the cmd environment is terminated and I'm back in my Git bash.
Could this be achieved in an easier way?
I tried cmd /C clean.bat since the docs say
Syntax
CMD [charset] [options]
CMD [charset] [options] [/c Command]
CMD [charset] [options] [/k Command]
Options
/C Run Command and then terminate
/K Run Command and then return to the CMD prompt.
This is useful for testing, to examine variables
Edit:
Just noticed that the post is broken.
What I want is to execute clean.bat from within the Git bash without having to type the three commands above (cmd, clear.bat, exit). I just want to execute the .bat file from within my Git bash. Obvious way would be to create a separate .sh file that does the same work but this will lead to double code.
Edit 2:
When I execute cmd /C clean.bat, the Git bash turns into a plain CMD environment and only displays the prompt. The file clean.bat does not get executed. It's the same as if I just type cmd.
Also, adding a /debug switch does literally nothing. Seems like only cmd gets evaluated and all further parameters are getting ignored.
After playing around a bit more, I found the solution myself:
cmd "/C clean.bat"
does the trick. But I got no clue, why...
./clear.bat will do the trick.
The Git for Windows (msysGit has been superseded by Git for Windows1) FAQ says you have 3 options:
Run programs that have problems using the winpty utility. This allows you to keep using the nicer mintty terminal, but can become unwieldy if you need the workaround for many programs.
Modify the shortcut for Git Bash to run bash directly without mintty so it uses the default console host and configure it for "Quick Edit", reasonable size and scroll-back and suitable unicode font. You'll still have to live with the other quirks of console host.
Install and use ConEmu.
At some point, Git for windows added support for the MSYS_NO_PATHCONV environment variable, so in addition to #eckes and #AlikElzin-kilaka solutions, you can also
MSYS_NO_PATHCONV=1 cmd /c clean.bat
In general, I prefer this solution, as it allows the code to be the closest to resembling normal bash, and there are many ways to export MSYS_NO_PATHCONV depending on your preferred situation.
Note: Git for Window's bash does not support the MSYS2 environment variable MSYS2_ARG_CONV_EXCL
The other solutions
The weird quoting solution
Why does cmd "/c clean.bat" not create other errors?
It turns out argument parsing in windows does not follow the same universal rules as it does in *nix. Instead, in windows the arguments are parsed differently based on the runtime you compile against. Basically in windows, the command line arguments are passed in as "one string" and then parsed by the runtime.
See here(archive) for more explanation than you could ever want.
E.g. cmd parses arguments differently then wscript.exe
In the end, you can hopefully find something that works with this method, and it is the most "window-esque" of the three solutions
The // method
This is pretty well explained here and simple to use, but adds an extra / which does not help readability
I like start clean, it opens a new window with cmd. This method has some benefits:
cmd.exe gets a native console
the new console has a native windows character encoding (e.g. cp1251 vs utf8)
This will work and it frees the terminal too
nohup ./nucleus.bat &
less nohup.out

Out of a git console: how do I execute a batch file and then return to git console?

I have a small utility script called clear.bat that does some housekeeping work on my sources.
It is a .bat file so that I could easily double-click it in Windows Explorer.
Sometimes, I find it more handy to execute it from my Git bash (msysgit, if this matters).
To do this, I type
cmd
clear.bat
exit
cmd turns my Git bash into a normal cmd window where I could easily execute my batch. When I type exit, the cmd environment is terminated and I'm back in my Git bash.
Could this be achieved in an easier way?
I tried cmd /C clean.bat since the docs say
Syntax
CMD [charset] [options]
CMD [charset] [options] [/c Command]
CMD [charset] [options] [/k Command]
Options
/C Run Command and then terminate
/K Run Command and then return to the CMD prompt.
This is useful for testing, to examine variables
Edit:
Just noticed that the post is broken.
What I want is to execute clean.bat from within the Git bash without having to type the three commands above (cmd, clear.bat, exit). I just want to execute the .bat file from within my Git bash. Obvious way would be to create a separate .sh file that does the same work but this will lead to double code.
Edit 2:
When I execute cmd /C clean.bat, the Git bash turns into a plain CMD environment and only displays the prompt. The file clean.bat does not get executed. It's the same as if I just type cmd.
Also, adding a /debug switch does literally nothing. Seems like only cmd gets evaluated and all further parameters are getting ignored.
After playing around a bit more, I found the solution myself:
cmd "/C clean.bat"
does the trick. But I got no clue, why...
./clear.bat will do the trick.
The Git for Windows (msysGit has been superseded by Git for Windows1) FAQ says you have 3 options:
Run programs that have problems using the winpty utility. This allows you to keep using the nicer mintty terminal, but can become unwieldy if you need the workaround for many programs.
Modify the shortcut for Git Bash to run bash directly without mintty so it uses the default console host and configure it for "Quick Edit", reasonable size and scroll-back and suitable unicode font. You'll still have to live with the other quirks of console host.
Install and use ConEmu.
At some point, Git for windows added support for the MSYS_NO_PATHCONV environment variable, so in addition to #eckes and #AlikElzin-kilaka solutions, you can also
MSYS_NO_PATHCONV=1 cmd /c clean.bat
In general, I prefer this solution, as it allows the code to be the closest to resembling normal bash, and there are many ways to export MSYS_NO_PATHCONV depending on your preferred situation.
Note: Git for Window's bash does not support the MSYS2 environment variable MSYS2_ARG_CONV_EXCL
The other solutions
The weird quoting solution
Why does cmd "/c clean.bat" not create other errors?
It turns out argument parsing in windows does not follow the same universal rules as it does in *nix. Instead, in windows the arguments are parsed differently based on the runtime you compile against. Basically in windows, the command line arguments are passed in as "one string" and then parsed by the runtime.
See here(archive) for more explanation than you could ever want.
E.g. cmd parses arguments differently then wscript.exe
In the end, you can hopefully find something that works with this method, and it is the most "window-esque" of the three solutions
The // method
This is pretty well explained here and simple to use, but adds an extra / which does not help readability
I like start clean, it opens a new window with cmd. This method has some benefits:
cmd.exe gets a native console
the new console has a native windows character encoding (e.g. cp1251 vs utf8)
This will work and it frees the terminal too
nohup ./nucleus.bat &
less nohup.out

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