Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should environment variables that contain a executable-path with spaces also contain the necessary quotes?

When defining an environment variable (on Windows for me, maybe there is a more general guideline)

set MY_TOOL=C:\DevTools\bin\mytool.exe

if the tool is located on a path with spaces

set MY_TOOL=C:\Program Files (x86)\Foobar\bin\mytool.exe

should the environment variable already contain the necessary spaces?

That is, should it read:

set MY_TOOL="C:\Program Files (x86)\Foobar\bin\mytool.exe"

instead of the above version without spaces?

Note: In light of Joeys answer, I really should narrow this question to the examples I gave. That is, environment variables that contain one single (executable / batch) tool to be invoked by a user or by another batch script.

Maybe the spaces should be escaped differently?

like image 621
Martin Ba Avatar asked Oct 10 '22 12:10

Martin Ba


1 Answers

I'd say, do it without quotes and use them everywhere you use the variable:

set MY_TOOL=C:\Program Files (x86)\Foobar\bin\mytool.exe

"%MY_TOOL%" -someoption someargument somefile

Especially if you let the user set the value somewhere I guess this is the safest option, since they usually tend not to surround it with quotes rather than do so.

If there are plenty of places where you use the variable you can of course redefine:

set MY_TOOL="%MY_TOOL%"

which makes things more resilient for you. Optionally you could detect whether there are quotes or not and add them if not present to be totally sure.

When your variable represents only a path to a directory and you want to append file names there, then the "no quotes" thing is even more important, otherwise you'd be building paths like

"C:\Program Files (x86)\Foobar\bin"\mytool.exe

or even:

""C:\Program Files (x86)\Foobar\bin"\my tool with spaces.exe"

which I doubt will parse correctly.

like image 141
Joey Avatar answered Nov 03 '22 23:11

Joey