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 ?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityTry 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 /?
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?
Jul 7, 2011 at 20:10
& was unexpected at this time
running your commands. OS: windows 10. Are you sure this is the correct way?
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%
doskey
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...
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.
time
does on Windows...