5

What I have to do currently:

cd C:\Program Files (x86)\MyProgram\modules\bin

What I'd like to do:

set dir as %Path%
cd %Path%

Is there a quick way to do this from the command prompt? Also, would I be able to call these path variables from Linux-based bash shells like the git bash shell or Cygwin?

2 Answers 2

3

That's not really how the path variable works.

If you add a directory to the path variable, you don't have to change to the directory to execute a program.

For example,

C:\>set Path=C:\Program Files (x86)\MyProgram\modules\bin;%Path%
C:\>program

will execute C:\Program Files (x86)\MyProgram\modules\bin\program.exe.

If you just want to store a directory's name in a variable (which shouldn't be Path), you can do this:

C:\>set myBin="C:\Program Files (x86)\MyProgram\modules\bin"
C:\>cd %myBin%
C:\Program Files (x86)\MyProgram\modules\bin>

If you want to set an environment variable for all command prompts (without having to enter set...), you can do so in

Computer [right click] -> Properties -> Advanced System Settings -> Environment Variables
1
  • 1
    I didn't realize the path variable had it's own specific use. I was just using it as a placeholder like %myVariable%. But The information is good to know despite. The second answer is what I was looking for, Thanks! Dec 17, 2011 at 20:26
0

In Command Prompt (cmd.exe), you can set environment variables using set:

set var=value
set "var=value"

and access them using %var%:

cd "%var%"

They will also be put into the environment of that cmd.exe process, and inherited by all processes you run from it. If you run bash inside this cmd window, it will have the same variable as $var. If you run python, it will have os.environ['var'], and so on.

However, environment variables can be only inherited. If you change a variable in cmd or bash, it will not update the environment of all other running processes, or those you start from Explorer, or those you start from Start menu.

The only exception to the above limitation is when the program can be told to re-read environment from another source; for example, if you use the environment configuration UI at Control Panel → System → Advanced System Settings → Advanced → Environment Variables, Explorer will re-read the environment from registry.

Finally, remember that %PATH% is a special variable, holding the locations of commands you execute. Don't set it to just anything; use %mydir% or whatever else instead.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .