How this windows command works: echo %path:;=&echo.%

This is using command-line variable substitution. %path:;=&echo.% means "%path%, but replace all ;s with &echo.". This means that, with set path=C:\Windows\System32;C:\Windows\;;C:\Python37;:

echo %path:;=&echo.%

becomes:

echo C:\Windows\System32&echo.C:\Windows\&echo.&echo.C:\Python37&echo.

Since & is a command separator, this is equivalent to:

echo C:\Windows\System32
echo.C:\Windows\
echo.
echo.C:\Python37
echo.

Due to quirks of DOS Batch, echo. is identical to echo except when there's nothing after it. If that's the case, it simply prints nothing, instead of telling you whether ECHO is on or off. This will make the output:

C:\Users\wizzwizz4> echo %path:;&echo.%
C:\Windows\System32
C:\Windows\

C:\Python37

C:\Users\wizzwizz4> 

Really, it should be echo.%path:;=&echo.% to account for the case where %PATH% starts with a ;, but this command is pretty clever anyway.


Getting into detailed detalias, really echo( should be used instead of echo.. This is because echo. can have problems when you've got a file called echo, and is slow because it has to check the disk (%CD% and I think also all of %PATH%) every time it runs. (I don't have a copy of Windows so I can't check it myself; is it just %CD% or anywhere in the %PATH% that the presence of the echo file will affect echo., and what does it do?)


That's an interesting solution that I've never seen before. Let me try to explain:

  1. To print the entire path, use echo %path%. This will print all directories on a single line separated with semicolons (;)
  2. To search / replace a string in a variable, use %path:a=b% which will replace all a characters with b
  3. echo. is used to print a newline
  4. & is used to separate commands, e.g. echo line1&echo line2 will print two lines
  5. In effect, semicolons in the path are replaced with a command to print a newline. Or maybe it is interpreted as 'replace ; with nothing, and then, print a newline'. I can't find any documentation on this, so it's just my interpretation. Frankly, I didn't even know that was possible, but there you go. UPDATE My interpretation of this step seems to be off, and is better explained by wizzwizz4.