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.

7
  • What exactly is wrong with the solution in your link?
    – DavidPostill
    May 21, 2016 at 15:03
  • ugly, too much symbols May 21, 2016 at 15:05
  • There isn't any other way ...
    – DavidPostill
    May 21, 2016 at 15:08
  • @DavidPostill please take a glance at my answer
    – JosefZ
    May 21, 2016 at 21:24
  • 1
    comment using env.bat by AFH in original question was writed AFTER my question. check timestamps. and he contain errors. May 22, 2016 at 8:44

1 Answer 1

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 ↑
6
  • 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, 2016 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, 2016 at 22:21
  • Please improve your answer is to use a multi-setting variables May 22, 2016 at 8: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 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, 2016 at 11:14
  • My questions will be closed in the same way people who think that I have to be satisfied with existing solutions. May 22, 2016 at 12:33

Not the answer you're looking for? Browse other questions tagged .