Searching for a solution to print pathes inside
path variable in windows Command-Line i came to this solution. the answer is this command:
now i wonder how this works.
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 community
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
echo.is used to print a newline
&is used to separate commands, e.g.
echo line1&echo line2will 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.
This is using command-line variable substitution.
%path:;=&echo.% means "
%path%, but replace all
&echo.". This means that, with
& 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?)
The interpreter works in 2 consecutive passes. Pass 1 performs all parameter substitutions. But a little-documented feature of parameter substitution is that for each individual parameter a secondary substitution is instructed with the syntax :STRING1=STRING2 after the parameter name. This is repeated multiple times if STRING1 appears more than once. Only then does Pass 2 interpret the line for execution. Try this for demystification: