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.
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.
That's an interesting solution that I've never seen before. Let me try to explain:
echo %path%
. This will print all directories on a single line separated with semicolons (;
)%path:a=b%
which will replace all a
characters with b
echo.
is used to print a newline&
is used to separate commands, e.g. echo line1&echo line2
will print two lines;
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.echo path
? are %% used for variables?
– yekanchi
Oct 23 '18 at 8:49
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 '18 at 8:51
$PATH
for instance.
– Berend
Oct 23 '18 at 8:57
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 '18 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?)
echo)
, but I don't really remember. ;-(
– wizzwizz4
Oct 23 '18 at 14:04
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 '18 at 14:25
echo(
is the only safe version.
– Neil
Oct 24 '18 at 15:17
echo.
may not work. – user202729 Oct 23 '18 at 14:46