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 /C "set "EDITOR=vim" && echo %EDITOR%" would not work.
cmd /C "setlocal ENABLEDELAYEDEXPANSION && set "EDITOR=vim" && echo !EDITOR!"
You would need:
/Voption, to enable delayed environment variable expansion using
- no space between a command and the
&&(or add quotes)
C:\> cmd /V /C "set EDITOR=vim&& echo '!EDITOR!'" 'vim' # or C:\> cmd /V /C "set "EDITOR=vim" && echo '!EDITOR!'" 'vim'
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.
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.
you can use ported util
env from package CoreUtils in GnuWin32 http://gnuwin32.sourceforge.net/
- Setup it
- Check what directory with
env.exeexists in %PATH% variable
- Use same way like linux version
env EDITOR=vim command
I have knocked up a batch file
env.cmd which works more or less like the Linux
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).
Vonc's answer will work for commands that reference the variable as expanded (that is
!FOO! instead of
However, It won't work if your command references a regular variable.
For example consider:
some-bat.bat (or any other executable/batch process)
And the main process:
set FOO=foo cmd /V /C "set FOO=bar && some-bat.bat"
foo instead of
bar (a second execution would work though)
But still, you could concatenate a new cmd process to force the refresh of the variable.
set "FOO=BAR" && cmd /c "echo %FOO%"
Or in case the main command already had to use a new cmd:
cmd /c "set FOO=BAR && cmd /c ^"echo %FOO%^""