Escape Ampersand in path variable

-1

In my PATH environment variable, there's

C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 & MySQL Utilities 1.5

This screws up the whole PATH --- the entries that come afterwards are not taken into account, and my shell does weird things:

C:\Users\bowi>echo %PATH%
C:\other;C:\path;C:\entries;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5
Der Befehl "MySQL" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
Der Befehl "MySQL" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

Obviously, the & in the path is the culprit, how can I escape it?

Bowi

Posted 2020-01-21T10:12:51.637

Reputation: 995

Answers

1

Just because you cannot echo %path% does not mean your PATH is broken. The purpose of the PATH environment variable is to provide a list of folders to search for a program if you invoke a program without specifying the path.

Assuming you have MyProgram.exe in the C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 & MySQL Utilities 1.5 folder, then executing MyProgram will correctly execute MyProgram.exe, regardless what your current directory is.

But indeed, if you attempt to echo %path%, then that will fail - and it should.

If you change the value in your PATH to read C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 ^& MySQL Utilities 1.5, then sure, you can safely echo %path%. But now if you execute MyProgram, then MyProgram.exe will not be found because the PATH is pointing to the wrong folder name.

An alternative that works is to modify your PATH to read "C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 & MySQL Utilities 1.5" (quote the value). Now the PATH functions properly, allowing you to execute MyProgram from anywhere, and it is now safe to echo %path%.

But you may not have control of all the values entered into your PATH definition. Because of the design of how PATH works, it simply is not safe to echo %path% in all situations. In fact, it simply is not reliable to ever use %path% in any command.

That is probably why the path command is designed to safely print out the value of the PATH environment variable without having to do any explicit variable expansion.

Even if echo %path% fails, executing path will print out the correct full definition.

Another option is to enable delayed expansion and use echo !path!. It is always safe to work with any value when you use delayed expansion.

dbenham

Posted 2020-01-21T10:12:51.637

Reputation: 9 212

In fact, echo %PATH% was not the main culprit, I am calling a lot of batch scripts from a cygwin shell and vice versa (without using %PATH%), and sometimes it bombed me with those Could not find command MySQL... outputs where I did not expect them. echo %PATH% was only the what I thought best way to easily reproduce the behaviour. I had tried the " way before, but I only clicked on OK once (see my answer), so it did not work. I now tried it once again and it worked. :-) – Bowi – 2020-01-23T08:38:45.733

0

Update: Don't do this, it only seems to work but lets the path point to the wrong directory (see answer by dbenham)!

I've found it! It's ^, and, most important, it seems to have no effect until you click OK in the environment variables overview window -- editing the variable and clicking OK in the editing box is not enough.

C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 ^& MySQL Utilities 1.5

Bowi

Posted 2020-01-21T10:12:51.637

Reputation: 995

That will allow you to echo %path%, but it breaks the main purpose of the PATH - the PATH variable would now be pointing to the wrong folder. – dbenham – 2020-01-22T15:40:06.933