I want to run a simple one-liner in the Windows CMD prompt to print my %PATH%
variable, one entry per line.
I tried this: for /f "delims=;" %a in ("%path%") do echo %a
but this only prints the first entry:
Z:\>for /f "delims=;" %a in ("%path%") do echo %a
Z:\>echo c:\python25\.
c:\python25\.
Also as you can see from the output above, this is also printing the echo %a
command as well as the output. Is there any way to stop this?
If I try a similar command, I get all the entries, but still get the echo %a
output spamming the results. I don't understand why the following prints all entries, but my attempt on %PATH%
doesn't. I suspect I don't understand the /F
switch.
Z:\>for %a in (1 2 3) do echo %a
Z:\>echo 1
1
Z:\>echo 2
2
Z:\>echo 3
3
Stephen Quan's answer is shorter and better, but here's a Python solution:
Turning
;
semicolons into\n
newlines.@ROMANIA_engineer proposed a PowerShell solution in a comment. Since the question asks for a command that works in the CMD shell, here is a way to use that elegant code from the OP's desired environment:
powershell -Command ($env:Path).split(';')
To make it still more readable, you can add sorting:
powershell -Command ($env:Path).split(';') | sort
Credit: https://stackoverflow.com/a/34920014/704808
This works in cmd window using Git Bash on Windows:
echo -e ${PATH//:/\\n}
You can also make a handy alias in your
.bash_profile
:alias showpath='echo -e ${PATH//:/\\n}'
An update to Stephan Quan's very clever one-liner solution: The problem I encountered was that a trailing semi-colon - (and maybe two successive semi-colons, i.e. empty path element) would cause the message "ECHO is on" to appear. I solved this by inserting a period immediately after the second ECHO statement (which is the syntax to suppress ECHO is on/off messages). However, it will result in an extra empty line:
A simple one liner to prettying printing the
PATH
environment variable:If your
PATH
was equal toA;B;C
the above string substitution will change this toECHO.A & ECHO.B & ECHO.C
and execute it all in one go. The full stop prevents the "ECHO is on" messages from appearing.The simple way is to use
This works for all without
;
in the path and without"
around a single elementTested with path=C:\qt\4.6.3\bin;C:\Program Files;C:\documents & Settings
But a "always" solution is a bit complicated
EDIT: Now a working variant
What did I do there?
I tried to solve the main problem: that the semicolons inside of quotes should be ignored, and only the normal semicolons should be replaced with
";"
I used the batch interpreter itself to solve this for me.
;
are replaced with^;^;
set var=%var:"=""%"
(The missing quote is the key!).This expands in a way such that all escaped characters will lose their escape caret:
var=foo & bar;;baz<>gak;;"semi^;^;colons^;^;^&embedded";;foo again!;;
...But only outside of the quotes, so now there is a difference between semicolons outside of quotes
;;
and inside^;^;
.Thats the key.