The Computer Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World

--

Chapters
00:00 Setting And Using Variable Within Same Command Line In Windows Cmd.Exe
00:24 Accepted Answer Score 89
01:48 Answer 2 Score 61
02:32 Answer 3 Score 13
03:02 Answer 4 Score 6
03:47 Answer 5 Score 5
04:25 Thank you

--

Full question
https://superuser.com/questions/223104/s...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows #commandline #shell

#avk47



ACCEPTED ANSWER

Score 89


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

You would need:

  • the /V option, to enable delayed environment variable expansion using ! as delimiter.
  • no space between a command and the && (or add quotes)

That is:

C:\> cmd /V /C "set EDITOR=vim&& echo '!EDITOR!'"
'vim'
# or
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.




ANSWER 2

Score 61


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.




ANSWER 3

Score 13


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



ANSWER 4

Score 6


Vonc's answer will work for commands that reference the variable as expanded (that is !FOO! instead of %FOO%)

However, It won't work if your command references a regular variable.

For example consider:

some-bat.bat (or any other executable/batch process)

echo %FOO%

And the main process:

set FOO=foo
cmd /V /C "set FOO=bar && some-bat.bat"

Returns 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.

Like this:

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%^""



ANSWER 5

Score 5


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).