20

In Linux (Bash), there's a way to use a command as a parameter for another command, using back-ticks:

> echo ===== `time` =====

This would print:

===== The current time is: 12:22:34.68 =====

Is there a way to do this in cmd.exe on WIndows ?

LotPings
  • 7,011
  • 1
  • 15
  • 29
Cristian Diaconescu
  • 3,632
  • 8
  • 29
  • 34

3 Answers3

12

Try this:

echo. ===== %time% =====

I know this may not be what you want, because you mentioned command substitution... So this may be it:

for /f "usebackq tokens=*" %i in (`date/time/t`) do @echo.  ===== %i =====

For more details about the usage of usebackq try this command:

for /?
Michael Burr
  • 532
  • 3
  • 11
Kurt Pfeifle
  • 12,411
  • 2
  • 54
  • 71
  • 4
    Yep, `for /f` is what I was looking for. Thanks! On a side note: It's so kludgy and hard to remember (compared to the bash way). I should give up "bat programming" and learn something more productive - PowerShell maybe? – Cristian Diaconescu Jul 07 '11 at 20:10
  • @CristianDiaconescu Why not just use Linux? It's a much superior OS to Windows, especially for devs. – Jack G Jul 18 '20 at 21:36
  • I am seeing errors that `& was unexpected at this time` running your commands. OS: windows 10. Are you sure this is the correct way? – jdhao Feb 20 '21 at 02:47
2

No, but here is the workaround:

D:\>time /t
08:18 PM

D:\>time /t > time.tmp

D:\>set /p time=<time.tmp

D:\>echo == %time% ==
== 08:18 PM ==

See also: Batch equivalent of Bash backticks.

kenorb
  • 24,736
  • 27
  • 129
  • 199
2

In Windows the '( )' operator has a similar behavior as the Bash command substitution.

This Linux script:

my_linux_variable=$(ls)
my_alternate_linux_variable=`ls`

echo $my_linux_command=$(ls)
echo $my_alternate_linux_command=`ls`

gives a similar result as Windows PowerShell:

$my_windowsPS_variable = (dir)

$my_windowsPS_variable

and as Windows CMD:

set my_windowsCMD_variable=(dir)
%my_windowsCMD_variable%
Kurt Pfeifle
  • 12,411
  • 2
  • 54
  • 71
DDS
  • 681
  • 8
  • 19
  • 14
    Windows CMD example is wrong. Parens in cmd shell just execute a command in a subshell. The env var is set to "(dir)" and it is executed on the second line (when the env var is surrounded by %) - not really the substitution you want. – davidbak Mar 02 '17 at 17:41
  • 4
    this is incorrect for windows cmd.exe behavior – JJS Jan 07 '19 at 18:33
  • 1
    This is powershell, whereas the question is about cmd.exe – Raúl Salinas-Monteagudo Jul 25 '19 at 08:37
  • neat trick; it's like passing by reference instead of by value: ie. the value of dir is actually processed/calculated in 2nd line, and the variable is storing the command, not the result of the command; similar to `doskey` – Zimba Aug 26 '21 at 17:39
  • `In Windows the '( )' operator has a similar behavior as the Bash command substitution.` this is absolutely wrong. Windows isn't a shell like bash, and cmd doesn't work that way. It should be *In powershell the '( )' operator...* – phuclv Jul 17 '22 at 05:22