0

Following How do I run two commands in one line in Windows CMD?,
It is natural to think that, if to set and use time in one line, it'll be

set cur_time=%time% && echo %cur_time%

However, to my greatest surprise, it is not the case at all. Take a look:

set cur_time=%time% && set cur_time && echo %cur_time%
cur_time=22:17:37.29
%cur_time%

set cur_time=%time% && set cur_time && echo %cur_time%
cur_time=22:17:40.09
22:17:37.29

set cur_time=%time% && set cur_time && echo %cur_time%
cur_time=22:17:52.25
22:17:40.09

set cur_time=%time% && set cur_time & echo %cur_time%
cur_time=22:19:37.50
22:17:52.25

I verified that the set cur_time output is correct.
However, why echo %cur_time% always output the previous value?
How to make set and use time variable in one line works?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
xpt
  • 20,363
  • 37
  • 127
  • 216
  • 1
    Type `set /?`, `setlocal /?`, `for /?`, and `cmd /?` for a discussion on *delayed expansion* of variables. Simple answer is do it on two lines. – user14122392 Nov 27 '20 at 04:20
  • To add detail. Batch files need to adhere to MS-Dos standards. So an IBM engineer added IBM mainframe type scripting to OS/2 using CMD as the program. So she overloaded most MS-Dos features into an MS-Dos version and an OS/2 version. All OS/2 version commands are opt-in to ensure compatibility. As MS-Dos does not support multiple commands a line, you have opted in for non standard behaviour. – user14122392 Nov 27 '20 at 05:47
  • 1
    Does this answer your question? [Example of delayed expansion in batch file](https://stackoverflow.com/questions/10558316/example-of-delayed-expansion-in-batch-file) or [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) – aschipfl Nov 27 '20 at 13:20
  • 1
    There is no need to use one line for the task you're performing, also your provided line is incorrect, irrespective of the reason why it isn't working. There is no reason why `set cur_time=%time%`, or `set cur_time` will fail, so there's no need to use `&&` instead of `&`. Additionally you should be using doublequotes, _or parentheses_, to ensure that you're not introducing unwanted space characters within your variable value. If you weren't interested in efficiency, for your specific batch file example, you could use `@Set "cur_time=%TIME%" & Set cur_time & Call Echo %%cur_time%%` instead. – Compo Nov 27 '20 at 13:53

1 Answers1

-1

Type set /?, setlocal /?, for /?, and cmd /? for a discussion on delayed expansion of variables.

Simple answer is do it on two lines.

To add detail. Batch files need to adhere to MS-Dos standards. So an IBM engineer added IBM mainframe type scripting to OS/2 using CMD as the program. So she overloaded most MS-Dos features into an MS-Dos version and an OS/2 version. All OS/2 version commands are opt-in to ensure compatibility.

As MS-Dos does not support multiple commands a line, you have opted in for non standard behaviour. That requires you to do other stuff to make it work.