Print out environment variable - cmd

I created an environment variable as follows:
setx HTTPS_PROXY "website"
And when I try to print it out:
echo %HTTPS_PROXY%
%HTTPS_PROXY%

PetSerAl's comment summarizes the reason from setx /?:
NOTE: 1) SETX writes variables to the master environment in the registry.
2) On a local system, variables created or modified by this tool
will be available in future command windows but not in the
current CMD.exe command window.
…
Solution: you need to use SET command, e.g. as follows.
set "HTTPS_PROXY=website"
setx HTTPS_PROXY "%HTTPS_PROXY%"
Note that (supposedly complex) website string is typed only once in above code snippet (to avoid a keying mistake) in so far that %HTTPS_PROXY% surely matches data in registry:
reg query hkcu\environment /V HTTPS_PROXY

Related

How can I set the environment variables in Windows?

Below is the Go code:
var (
Address = os.Getenv("ADDR")
Token = os.Getenv("TOKEN")
)
It reads the environment variables in Windows.
On a Windows laptop, I have the privilege to set user variables for my user login only. I created two variables (for the above), but os.Getenv() cannot read the values.
I do not have the privilege to set system variables.
How can I set environment variables in Windows, with my user login?
In Windows, environment variables can be applied in two ways.
Set modifies the current shell's (the window's) environment values, and the change is available immediately, but it is temporary. The change will not affect other shells that are running, and as soon as you close the shell, the new value is lost until such time as you run set again.
cmd> SET ADDR=127.0.0.1
cmd> SET TOKEN=ABCD1234
cmd> SET
setx modifies the value permanently, which affects all future shells, but does not modify the environment of the shells already running. You have to exit the shell and reopen it before the change will be available, but the value will remain modified until you change it again.
cmd> setx ADDR "127.0.0.1"
cmd> setx TOKEN "ABCD1234"
cmd> SET
You can try using the set method in a terminal in the following way:
set NODE_HOME=C:\Users\359855\Downloads\node-v14.16.0-win-x64
set PATH=%PATH%;%NODE_HOME%;
I don't know if this is the same as yours, but I use: os.environ.get('environment variable').
Also you can add a print(environment variable) and run to check if everything is fine. Let's say the environment variable is SECRET_KEY.
You add a print(SECRET_KEY) to your line of code and run, check your terminal for possible results.

Session Environment Variables for Windows CMD shell

I understand from my googling that on *nix systems you can use the export command to set a temporary environment variable for the current session. What's the Windows equivalent for the plain old CMD shell?
I finally figured out that SET is the equivalent of export.
Furthermore, to reference the variable, it needs to be surrounded by percent signs:
SET MY_VARIABLE=42
echo %MY_VARIABLE%
SET documentation: ss64.com/nt/set.html

Choose whether variable is a user or system environment variable in Batch?

I only have headless access to a windows machine, and I need to specifically set a variable as a user environment variable (cannot be system). I would also like to set it permanently, and not just for the cmd session. Is there a way of doing this just through the command prompt?
Expanding #Mofi's comment :
Using setx you can set a variable as persistent (like system variable)
but it will only be avaiable on the next run of a CMD Interpreter.
So if you need to use it in the actual handle of the CMD too,
you'll better set it twice :
example :
#echo off
set "MyVar=test1234"
setx MyVar test1234
Like this it will be avaiable NOW and for the NEXT CMD sessions

Are environment variables expanded when searching PATH?

This question expands on the comments on this deleted answer. I claimed that an unexpanded variable reference in the PATH would not be expanded when searching for an executable, but Ken said he didn't see the same behaviour that I did.
Note that the ordinary situation is subtly but critically different: subject to certain conditions, environment variables are automatically expanded when the PATH environment variable is built from the information in the registry. I'm talking about the case where, for one reason or another, this hasn't happened, so the actual environment block of the cmd.exe process contains a PATH which still has environment variable references in it.
Here is the code I built to test this behaviour:
md test1
echo echo hello! > test1\test1.cmd
set TESTPATH=%cd%\test1
set percent=%%
set PATH=c:\windows\system32;c:\windows;c:\windows\system32\Wbem;%percent%TESTPATH%percent%
set PATH
set TESTPATH
test1
cmd /c test1
start test1.cmd
and this is the result on my machine:
C:\working\testpath>test
C:\working\testpath>md test1
C:\working\testpath>echo echo hello! 1>test1\test1.cmd
C:\working\testpath>set TESTPATH=C:\working\testpath\test1
C:\working\testpath>set percent=%
C:\working\testpath>set PATH=c:\windows\system32;c:\windows;c:\windows\system32\
Wbem;%TESTPATH%
C:\working\testpath>set PATH
Path=c:\windows\system32;c:\windows;c:\windows\system32\Wbem;%TESTPATH%
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw
C:\working\testpath>set TESTPATH
TESTPATH=C:\working\testpath\test1
C:\working\testpath>test1
'test1' is not recognized as an internal or external command,
operable program or batch file.
C:\working\testpath>cmd /c test1
'test1' is not recognized as an internal or external command,
operable program or batch file.
C:\working\testpath>start test1.cmd
The system cannot find the file test1.cmd.
What is the expected behaviour? Does it vary depending on the version of Windows and/or other factors?
There are two entirely different points of view in this question:
Q: How to expand the values of variables embedded in another one? This point is not specific to PATH variable, but works on anyone.
A: Enclose the names of the variables in exclamation marks and enable delayed expansion when you want to expand such values:
#echo off
setlocal EnableDelayedExpansion
set TESTPATH=%cd%\test1
set "PATH=c:\windows\system32;c:\windows;c:\windows\system32\Wbem;^!TESTPATH^!"
set PATH
echo PATH=%PATH%
Q: Does cmd.exe such delayed expansion when it use PATH to locate an executable file?
A: No. cmd.exe uses the values in PATH variable as they appear, with no further processing. Any special character that may appear in PATH, like percents or exclamation-marks, are taking literally.

Invalid syntax with setx

I used the setx command to set OGRE_HOME:
setx OGRE_HOME D:\Program Files\OgreSDK
Now I need to change to value of OGRE_HOME.
How can I search all the values I have set?
If I run the command again, it shows that:
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Your path to the Ogre SDK has a space character in it, which is interpreted as a delimiter to another argument. Surround your path with " to keep it as one single argument to setx:
setx OGRE_HOME "D:\Program Files\OgreSDK"
To see the current value of the OGRE_HOME environment variable:
echo %OGRE_HOME%
You may have to open a new command prompt shell to see the value if you set it and are then trying to immediately see it's value.
To see all currently set environment variables, simply run:
set
To show only environment variables that have a certain prefix (so FOO would show FOOBAR and FOOBAZ), put that prefix after set:
set PREFIX
Alternatively, you can use the GUI to edit environment variables (assuming Windows 7 here).
Right-click Computer, choose Properties
Click Advanced system settings in the left pane
Make sure you're on the Advanced tab in the pop-up dialog
Click Environment Variables... at the bottom
A dialog will pop up with your user-specific environment variables as well as your system-wide environment variables. Select a value and use the New/Edit/Delete buttons to interact with them.
Command Prompt is giving you that error because you forgot the quotation marks. You should’ve typed:
setx OGRE_HOME "D:\Program Files\OgreSDK"
To see all the values you’ve already set, enter either:
reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
OR
reg query HKEY_CURRENT_USER\Environment
setx and pretty much all windows command line commands are sensitive to certain special characters. Among them the space character but there's also the quote which is used to delimit an entry.
As #ajp15243 already said, you can deal with the space by locking off the path{s) between two quotations. But what if you have paths and those path already have quotations because they carry a space? Here's an example:
MY_PATHS="c:\Program Files\path1";"c:\Program Files(x86)\Path2"
In this case, you would have to put escape characters for those inner quotation marks when you use setx or it will get confused and give the error you listed. Eg:
setx -m MY_PATHS "\"c:\Program Files\path1\";\"c:\Program Files(x86)\Path2\""
As an addendum to #ajp15243's answer. If you are doing the same with PowerShell rather than the command prompt or batch file, you'll need to call SETX with a leading escaped double-quote character, as in:
$my_path = "%PROGRAMFILES%\MySQL\MySQL Server 5.7\bin\"
$hkcu_path = (Get-ItemProperty hkcu:\Environment).PATH + ";" + $my_path
SETX PATH "`"$hkcu_path" # note the leading escaped quote
However doing so, may result in adding a trailing double quote in the value of hkcu:\Environment\PATH, so you may need to do this too:
$dirty_path = (get-itemproperty hkcu:\Environment).PATH
$clean_path = $dirty_path -replace '"',''
SETX PATH $clean_path

Resources