Setting and using variable within same command line in Windows cmd.exe

80

21

In Bash, I can do EDITOR=vim command and command will be run with EDITOR set to vim, but this won't affect the value of EDITOR in the shell itself. Is it possible to do this in cmd.exe?

Alexey Romanov

Posted 2010-12-17T15:04:06.730

Reputation: 1 698

Answers

57

Note that cmd /C "set "EDITOR=vim" && echo %EDITOR%" would not work.
Nor would cmd /C "setlocal ENABLEDELAYEDEXPANSION && set "EDITOR=vim" && echo !EDITOR!"

What would is the /V option, to enable delayed environment variable expansion using ! as delimiter.

C:\> cmd /V /C "set EDITOR=vim&& echo !EDITOR!"
vim

As noted below by maoizm, it is cmd /V /C, not cmd /C /V (which would not work)


I can't think of any practical reason you'd ever actually want this within the context of a single command

Typically, you need this when you have to replace a value used multiple times in a long command line.
For instance, to deploy a file to Nexus (in multiple lines for readability):

cmd /v /c "set g=com.agroup&& set a=anArtifact&& set v=1.1.0&& \
           mvn deploy:deploy-file -Dfile=C:\path\!a!-!v!.jar \
           -Dpackaging=jar -DgroupId=!g! -DartifactId=!a! -Dversion=!v! \
           -DrepositoryId=nexus 
           -Durl=http://myserver/nexus/content/repositories/my-repo/"

Instead of having to replace group, artifact (used 2 times) and version in a long and complex command line, you can edit them at the beginning of said command. It is clearer/easier to manipulate and change the parameter values.

VonC

Posted 2010-12-17T15:04:06.730

Reputation: 13 292

1Tested it on my machine with Windows 10 version 1607, the cmd prompt crashes. – Fernando Costa Bertoldi – 2016-12-21T20:37:44.673

Note that EDITOR will not be set to 'vim' but 'vim ' in the last command. Add the internal quotes to avoid the space in the end. – Zitrax – 2017-02-03T11:57:25.107

@Zitrax Right! I have edited the answer accordingly. – VonC – 2017-02-03T11:59:48.023

Based on given solution I created a way to access ERRORLEVEL of two commands that run on same command line: cmd /v /c "dir file1.txt >NUL 2>&1 & echo !ERRORLEVEL! & dir file2.txt >NUL 2>&1 & echo !ERRORLEVEL!" – sactiw – 2017-05-10T19:37:18.853

1NB: interesting: the order of switches matters in this case. cmd /v/c "..." works, but cmd /c/v "..." fails -- hopefully it can save you 15 minutes – maoizm – 2017-06-04T11:12:23.077

This works and is clever, but it's unnecessary syntax noise unless you actually need parameter expansion. I can't think of any practical reason you'd ever actually want this within the context of a single command. If you know the value of EDITOR at the time the command is being executed, you'd just use it. Now if you were going to use it multiple times and the effort of typing the variable name were less than typing the actual value then it could be useful. – Derek Greer – 2017-07-20T23:22:39.790

1@DerekGreer " I can't think of any practical reason": OK, I have edited the answer to add a practical reason. – VonC – 2017-07-21T06:20:00.430

Now that I see it actually used in the only way I thought it might be useful, I'm not sure even that is practical. The example you've provided would likely never be typed as an ad-hoc command, but rather would most generally be in a script where you'd have the opportunity to set variables to avoid such repetition as well as improve readability. It's still clever though. – Derek Greer – 2017-07-21T13:54:54.523

@DerekGreer: "I'm not sure even that is practical." An even more common example is commands in makefiles. The command is "in a script", for sure, but not at home, in a CMD batch file... Insanely harsh environment, either way (make or cmd), BTW, especially when combined!. :) – Sz. – 2019-08-18T17:03:25.937

1

@DerekGreer This is also useful (and commonly used in *Nix) for apps that read environment variables. (e.g. EDITOR=emacs git commit) On windows, it could also be used to load DLLs from a custom location, by setting DEVPATH https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/how-to-locate-assemblies-by-using-devpath?redirectedfrom=MSDN

– iCodeSometime – 2020-01-23T18:32:46.410

51

You can do it in windows like this no need for installing anything.

cmd /C "set EDITOR=vim && set"

You'll see a list of variables and you'll see EDITOR=vim, now run "set" again and it won't be listed.

You can do multiple &&'s to add additional commands:

cmd /C "set EDITOR=vim && do this && do that && otherstuff"

EDIT: /C exits the new cmd right away after running, if you produce output with the new one it will still be visible in the parent window.

You can opt to use /K in which case the new cmd window stays open at the end of the run.

Viper_Sb

Posted 2010-12-17T15:04:06.730

Reputation: 936

7Be careful. Setting the variable this way will result in a trailng space in the variable EDITOR: "vim ". To avoid the trailing space use: cmd /C "set "EDITOR=vim" && do this && do that" – Gerd K – 2015-04-03T12:00:07.070

1This is not working on "Windows Server 2008", for example. I tried this: set name="username" && echo %username%. And username is empty. – akozin – 2015-05-15T12:10:54.373

@akozin Do you realize that you got the keyvalue backwards? Did you mean set name=foo&&echo %name% or set username=foo&&echo %username%? – Phrogz – 2015-07-30T23:00:56.323

@akozin see my answer below

– VonC – 2015-08-05T06:03:08.337

8

you can use ported util env from package CoreUtils in GnuWin32 http://gnuwin32.sourceforge.net/

  1. Setup it
  2. Check what directory with env.exe exists in %PATH% variable
  3. Use same way like linux version env EDITOR=vim command

Red Skotina

Posted 2010-12-17T15:04:06.730

Reputation: 83

maybe worth saying that if you have git/docker/msys/cygwin you'll already have a version of this. – xenoterracide – 2018-02-26T16:29:59.760

3

I have knocked up a batch file env.cmd which works more or less like the Linux env command:-

echo off
setlocal
for %%f in (%*) do (
  echo %%f|find "=" >nul:
  if errorlevel 1 goto DoCmd
  set %%f
  shift
)
:DoCmd
%1 %2 %3 %4 %5 %6 %7 %8 %9
endlocal

The only difference is that, because of the way cmd parses, the environment assignments need to be quoted, so your command would be:

env "EDITOR=vim" command [up to 8 parameters]

The batch file could be elaborated to remove the 8-parameter restriction by building up the command string within the for loop (delayed expansion will be needed).

AFH

Posted 2010-12-17T15:04:06.730

Reputation: 15 470

Why not just use %* as the penultimate line (i.e., the one after the :DoCmd label) to allow for very long user commands? – Scott – 2016-05-22T23:47:53.020

@Scott - Because %* is the original parameter list: it is unaffected by shift (unlike $* in Unix). – AFH – 2016-05-23T00:34:59.647