8

In Bash I can create TEMPORARY environment variables on a command line. For eample

DEBUG=foo somecommand 

This sets the environmnet variable DEBUG but only for somecommand. When the line is finished DEBUG is no longer set.

Can I do something similar in the Windows Command Processor?

Note: Using SET does not work. That sets the current command processors environment, not the just for the command about to be executed.

To give another example here's a small node.js program that prints the value of a single environment variable

// test.js console.log(`${process.argv[2]}='${process.env[process.argv[2]]}'`); 

Let's run it in bash

$ export FOO=abc $ node test.js FOO FOO='abc' 

Then let's run it with a temporary setting

$ FOO=def node test.js FOO FOO='def' 

Check that FOO is still abc

$ echo $FOO abc 

How I can accomplish the same thing in the Windows command prompt?

One way seems to be to relaunch the command processor as in

cmd /S /C "set "FOO=def" & node test.js FOO" 

Is there another way or is that it?

1
  • "Is there another way or is that it?" I think that is the only way. set foo=bar && echo %foo% && set foo= && echo %foo% does not work :( Commented Jan 28, 2018 at 7:32

2 Answers 2

5

Simply use the set command:

C:\>set foo=bar C:\>echo %foo% bar C:\>exit 

Open cmd again

C:\>echo %foo% %foo% C:\> 
2
  • why do i keep getting notifications for this? Commented Jun 28, 2021 at 7:40
  • This doesn't do what the OP is asking. In Linux you can set the value of an environment variable for the executed command only. It can be an environment valiable that exists and it doesn't get overriden for other processes. With set, you change the value of the environment variable FOR ALL processes in that session. Commented Oct 22, 2022 at 4:52
0

This can be done in a script with SETLOCAL and ENDLOCAL

EXAMPLE:

SETLOCAL set FOO=abc node test.js FOO ENDLOCAL 

If you weren't running a script, you wouldn't need the variable you would simply:

node test.js abc 

If you are generating commands programmatically, generate the script and run that, deleting it afterwards. I know, not ideal.

3
  • This didn't work for me i.sstatic.net/MZIzj7pB.png It's also not really an answer to the actual question. If I have to write a script to do it that's not really an answer. Also your comment about passing an argument is beside the point and off topic. The node command is just an example. The program reading the environment variable is not under my control. Commented May 6, 2024 at 6:36
  • We are talking about DOS here. It doesn't have the same capabilities as bash. You may have to get creative. Many such strange workarounds are common when using legacy DOS systems. Your other option is to use a more modern command interpreter that is still under support, like PowerShell. You can also run bash on Windows now. Commented May 6, 2024 at 17:18
  • @HackSlash PowerShell does not support such a syntax either, do it? It always presumes that the first symbol is the command. Commented May 24, 2024 at 6:30

You must log in to answer this question.