Searching for a solution to print pathes inside path variable in windows Command-Line i came to this solution. the answer is this command:

echo %path:;=&echo.%

now i wonder how this works.

up vote 20 down vote accepted

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.
  • what is %% signs used for, can;t we tell echo path? are %% used for variables? – yekanchi Oct 23 at 8:49
  • 1
    Yes, that is correct, those are used to get the value of a variable. For instance: set greeting=Hello and then echo %greeting%. Confusingly, PATH happens to be both a command and a variable, so typing the command path, or echo %path% will have the same result. – Berend Oct 23 at 8:51
  • what this syntax is called? is it regex? or something used in windows CMD only – yekanchi Oct 23 at 8:52
  • I'm not sure if there's a name for this, it is just the way variables work in DOS. Other languages, such as bash, use $PATH for instance. – Berend Oct 23 at 8:57
  • 2
    This is nothing to do with DOS, Berend; and echo. is not "the command to print a newline". This is Microsoft's command interpreter for Windows NT, cmd; and the echo. is simply the command to print a path element ensuring that if the path element is empty it does not switch to the other functionality of echo. The first echo really should be an echo. too. – JdeBP Oct 23 at 11:29

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

  • This is long-standing behaviour of Microsoft/IBM command interpreters across several operating systems, and is not specific to batch scripts. As I noted at superuser.com/a/310157/38062 years ago, there are several punctuation characters in addition to whitespace that count as command-name termination characters. – JdeBP Oct 23 at 14:03
  • @JdeBP I think one of the more efficient ones is echo), but I don't really remember. ;-( – wizzwizz4 Oct 23 at 14:04
  • 3
    echo. is the one that people used to circulate the most, years ago. It is the one in many books. Efficiency is not the problem. Wacky behaviour when various files happen to exist is. When I wrote a 32-bit replacement for the 16-bit cmd in OS/2, breaking command names at these punctuation characters and this highly irregular parsing behaviour is one of the things that I deliberately did not duplicate, and which I put on the documented list of differences. I made an external echodot command instead, which one could alias as echo. as desired. – JdeBP Oct 23 at 14:25
  • @JdeBP That sounds really interesting! Do you think you could document some of that? – wizzwizz4 Oct 23 at 14:31
  • If you follow various comment links you end up at a third-party forum message claming that echo( is the only safe version. – Neil Oct 24 at 15:17

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.