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
Related
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.
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
Assuming I have the following set as system environment variables in Windows 7
FOO = foo
path = ...;%FOO%/bin
Take the following example run in cmd
set FOO=bar
start cmd
echo %FOO%
//bar
echo %path%
//...;foo/bin
The path environment variable did not re evaluate itself upon launch of cmd, however the new FOO variable did stick. How can I get path to reevaulate itself based on the new FOO variable set in the parent command terminal?
EDIT: I'm looking for path to become ...;bar/bin
There is no "command line equivalent of opening cmd.exe from the desktop or start menu" that reproduces the behavior you care about. Environment variables are inherited from the parent process.
The shell reads the registry and performs interpolation (using its own environment, which is the process of being read from the registry, and knows nothing of variables you set in a command interpreter), which is why updates to the registry settings are reflected in a cmd.exe launched from the shell.
If you launch a new cmd.exe from a running cmd.exe, you won't get the shell behavior, and you will get the existing environment inherited. There's nothing in Windows that uses variables in a command interpreter to interpolate the registry settings. The code responsible for reading the environment from the registry is completely unrelated to cmd.exe... it is in explorer.exe (or probably one of the shell DLLs used by explorer).
This answer, which uses VB Script to read the registry and construct a batch file, is as good as you can get. I haven't tested whether interpolation is performed in the registry-access COM component (Environment("System") method on a WScript.Shell object) used by VB script, or if the environment variable references survive into the batch file and are interpolated during batch processing. So you may be confounded by the order of evaluations and variable assignments, in which case you'd better adapt the script to fetch just the PATH setting itself and leave all other variables alone.
I also found this to be useful
set path=%path:foo/bin=bar/bin%
Not as dynamic as I wanted but it works to replace a portion of a variable.
Can someone please tell me what's the correct way to set a bunch of environment variables in the fish shell?
In my ~/.config/fish/config.fish file, I have a function to setup my environment variables like so:
function setTESTENV
set -x BROKER_IP '10.14.16.216'
set -x USERNAME 'foo'
set -x USERPASS 'bar'
end
When I type from the command prompt setTESTENV and do a env in the command line, I don't see this information.
Use Universal Variables.
If the variable has to be shared between all the current user Fish instances on the current computer and preserved across restarts of the shell you can set them using -U or --universal. For example:
set -Ux FOO bar
Using set with -g or --global doesn't set the variable persistently between shell instances.
Note:
Do not append to universal variables in config.fish file, because these variables will then get longer with each new shell instance. Instead, simply run set -Ux once at the command line.
Universal variables will be stored in the file ~/.config/fish/fish_variables as of Fish 3.0. In prior releases, it was ~/.config/fish/fishd.MACHINE_ID, where MACHINE_ID was typically the MAC address.
The variables you are declaring are keep in a local scope inside your function.
Use:
set -g -x
Here "g" is for global.
another option is to run:
export (cat env_file.txt |xargs -L 1)
where env_file.txt contains rows of the format VAR=VALUE
this has the benefit of keeping the variables in a format supported by other shells and tools
Environment Variables in Fish
I would like to add that, while #JosEduSol's answer is not incorrect and does help solve the OP problem, -g is only setting the scope to be global, while -x is causing the specified environment variable to be exported to child processes.
The reason the above fails, is because #cfpete is setting the env vars inside a function and the default scope will be local to that function.
I happen to run some commands blindly, in order to get things done.
I started to work with Jenkins recently, and then I had to use this export command to run the Jenkins WAR archive. What does the export command do in general, and why do we need to run this command, while running Jenkins (after the Jenkins home is set)?
export in sh and related shells (such as Bash), marks an environment variable to be exported to child-processes, so that the child inherits them.
export is defined in POSIX:
The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to word.
I guess you're coming from a Windows background. So I'll contrast them (I'm kind of new to Linux too). I found a user's reply to my comment, to be useful in figuring things out.
In Windows, a variable can be permanent or not. The term environment variable includes a variable set in the cmd shell with the SET command, as well as when the variable is set within the Windows GUI, thus set in the registry, and becoming viewable in new cmd windows.
E.g., the documentation for the set command in Windows "Displays, sets, or removes environment variables. Used without parameters, set displays the current environment settings."
In Linux, set does not display environment variables. It displays shell variables which it doesn't call/refer to as environment variables. Also, Linux doesn't use set to set variables (apart from positional parameters and shell options, which I explain as a note at the end), only to display them and even then only to display shell variables. Windows uses set for setting and displaying, e.g., set a=5, but Linux doesn't.
In Linux, I guess you could make a script that sets variables on bootup, e.g., /etc/profile or /etc/.bashrc, but otherwise, they're not permanent. They're stored in RAM.
There is a distinction in Linux between shell variables, and environment variables. In Linux, shell variables are only in the current shell, and environment variables, are in that shell and all child shells.
You can view shell variables with the set command (though note that, unlike Windows, variables are not set in Linux with the set command).
set -o posix; set (doing that set -o posix once first, helps not display too much unnecessary stuff). So set displays shell variables.
You can view environment variables with the env command.
Shell variables are set with, e.g., just a = 5.
Environment variables are set with export. Export also sets the shell variable.
Here you see shell variable zzz set with zzz = 5, and see it shows when running set, but it doesn't show as an environment variable.
Here we see yyy set with export, so it's an environment variable. And see it shows under both shell variables and environment variables:
$ zzz=5
$ set | grep zzz
zzz=5
$ env | grep zzz
$ export yyy=5
$ set | grep yyy
yyy=5
$ env | grep yyy
yyy=5
$
Other useful QnAs:
https://unix.stackexchange.com/questions/176001/how-can-i-list-all-shell-variables
https://askubuntu.com/questions/26318/environment-variable-vs-shell-variable-whats-the-difference
Note: One point which elaborates a bit and is somewhat corrective to what I've written, is that, in Linux bash, 'set' can be used to set "positional parameters" and "shell options/attributes", and technically both of those are variables, though the man pages might not describe them as such.
But still, as mentioned, set won't set shell variables or environment variables). If you do set asdf then it sets $1 to asdf, and if you do echo $1 you see asdf.
If you do set a=5 it won't set the variable a, equal to 5. It will set the positional parameter $1 equal to the string of "a=5". So if you ever saw set a=5 in Linux it's probably a mistake unless somebody actually wanted that string a=5, in $1.
The other thing that Linux's set can set, is shell options/attributes. If you do set -o you see a list of them. And you can do for example set -o verbose, off, to turn verbose on (by the way, the default happens to be off, but that makes no difference to this). Or you can do set +o verbose to turn verbose off. Windows has no such usage for its set command.
In simple terms, environment variables are set when you open a new shell session. At any time if you change any of the variable values, the shell has no way of picking that change. That means the changes you made become effective in new shell sessions.
The export command, on the other hand, provides the ability to update the current shell session about the change you made to the exported variable. You don't have to wait until new shell session to use the value of the variable you changed.