2

I'm looking for an opportunity to launch an application (eg, compilers) with specific environment variables . I want same functionality like env in Linux. env var=value application.exe

I found Setting environment variable for just one command in Windows cmd.exe but not satisfied with the decision. (because it has a lot of character in the writing which you can make a mistake )

Something changed from 2010 year ? I would be happy not even with builtin solutions.

1 Answers1

3

env var=value application.exe

is possible. Write following batch script, name it env.bat and place it somewhere in a directory from your PATH variable. Please read Command Line arguments (Parameters) and setlocal articles for further explanation.

env.bat script:

@setlocal
@set "%~1=%~2"
@%3

Above script applies to given simple command but would require to be a bit elaborated to accept line parameters like env var=value application.exe action input output etc.


Before edit:

set var=value&application.exe&set var=

i.e. set variable then run a console application and then remove variable.

Another approach:

cmd /C "set var=value&application.exe"

i.e. start a new instance of the Windows command interpreter and within it set variable then run a console application; for /C switch meaning see cmd /?:

/C      Carries out the command specified by string and then terminates

Here & ampersand must be escaped in the command specified. You could choose any:

cmd /C "set var=value&application.exe"

or

cmd /C set var=value^&application.exe

Note proper spacing as well:

                       ↓ allowed space                    
cmd /C "set var=value & application.exe"
                     ↑  this is harmful space as "%var%" would be "value " then
                                                    note trailing space ↑
JosefZ
  • 13,217
  • Just a quick comment: If value contain a ; colon (command separator) then env.bat fails to work correctly, eg env var=ab;cdef or env var=%path%. The value of var gets truncated at the ; and the rest of the parameters get messed up. I don't know how to easily fix that. – DavidPostill May 21 '16 at 21:52
  • @DavidPostill Semicolon is a delimiter in cmd and should be escaped using double quotes as follows: env var="ab;cdef". BTW, a script can't be concurrently simple and bullet-proof (if we can believe in existence of such oxymoronic thing as "bullet-proof script" . – JosefZ May 21 '16 at 22:21
  • Please improve your answer is to use a multi-setting variables – Red Skotina May 22 '16 at 08:16
  • 2
    @RedSkotina are you asking for a fully functional emulator of Linux and Unix env command? This would be a challenging task but requires herculean effort… Such task transcends current question topic. Moreover, let's consider current thread closed (duplicate at present). Please ask a new question at http://stackoverflow.com/ having in mind their How do I ask a good question and How to create a Minimal, Complete, and Verifiable example guide. Show your own effort… – JosefZ May 22 '16 at 11:14
  • My questions will be closed in the same way people who think that I have to be satisfied with existing solutions. – Red Skotina May 22 '16 at 12:33
  • The behaviour of the above solution does not truly emulate the behaviour of the Unix env command, becuase it spawns a subprocess i.e. cmd.exe -> application.exe Whereas env uses the exec* family of system calls to replace the running process env.exe with the new application.exe process – jacob_pro May 30 '22 at 15:40