54

I'm trying to extend my $PATH variable in git bash (MinGW shell) by adding the following to the file ~/.bashrc

PATH=$PATH':/c/Program Files/maven/apache-maven-3.2.5/bin'

After I did this and restarted the bash it seems like that the $PATH variable was extended like expected:

$ echo $PATH
MANY_OTHER_PATHS:/c/Program Files/maven/apache-maven-3.2.5/bin

But I still cannot execute the programms in the given directory:

$ mvn
bash: mvn: command not found

What went wrong here? How do I extend the PATH variable correctly?

tommybee
  • 2,409
  • 1
  • 20
  • 23
eztam
  • 3,443
  • 7
  • 36
  • 54

4 Answers4

49

Here are two ideas.

You can have your path with double quote mark.

export PATH=$PATH:"/C/Program Files (x86)/apache-maven-3.3.3/bin"

enter image description here

Or, You can also make symbolic link for the directory.

ln -s "/C/Program Files (x86)/apache-maven-3.3.3/bin" ./mvnbin
export PATH=$PATH:/your-path/mvnbin

enter image description here

It works for me in mingw32 environment.

Matthew
  • 1,905
  • 3
  • 19
  • 26
tommybee
  • 2,409
  • 1
  • 20
  • 23
  • 1
    You could also create an alias (basically equivalent to symlink). – EvanM Aug 31 '17 at 23:30
  • 1
    Okey, it can be an option thanks but also you can answer him – tommybee Sep 01 '17 at 00:00
  • When I run the first command (to add a directory to `PATH`) I get an unhelpful error "`bash: /c/my/path: Is a directory`" (of course it's a directory!) even though my syntax is the same as yours (just a different folder path). Any idea? – Dai Mar 06 '20 at 03:01
  • Making it an alias and then adding that to the path worked for me. Just add the individual binaries to the path with aliases. – Entalpi Nov 23 '21 at 15:50
15

I needed to add something to my Git Bash path permanently each time I open it. It was Meld.exe path which can be added with:

export PATH=$PATH:"/C/Program Files (x86)/Meld/lib"

In order to execute this command each bash session, you need a ~/.bashrc file. Check if it already exists or create it using notepad ~/.bashrc or touch ~/.bashrc.

You can check where it is with:

echo ~

Open it and add the command that adds the PATH (first command in this response).

I hope you found this useful.

David L.
  • 191
  • 1
  • 4
9

Add PATH in Git Bash Permanently | Windows Only

Just in case you are still wondering how to add a path permanently in git bash here is the step-by-step process for Windows users:

  1. Create .bashrc in user's root folder using the below command. It will open notepad and ask you to create the file, click yes.
    notepad ~/.bashrc
    
  2. Put the directory you want to add as below, for more than 1 items repeat the same format in next line:
    export PATH=$PATH:"/c/folder/folder/"
    
  3. Save the file and relaunch the bash.
  4. Next launch will give you a warning like WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile. but git bash will handle it by creating the required files.

SOME INSIGHTS

  1. Git Bash doesn't fetch Window's environment PATH, it maintains its PATH separately in more like a Linux way.
  2. You can run export PATH=$PATH:"/c/folder/folder/" in cmd to add a directory to path, but it will be only for the current session once you close the bash, it will be gone.
  3. .bashrc is a shell script file that will be executed every time you launch a new git bash window. So you can add any type of bash command here. We simply added the export command to add our desired directory to PATH.
Abhishek Sachan
  • 1,976
  • 2
  • 18
  • 33
2

According to this SO post, you need to escape Program Files with quotes. git-bash $PATH cannot parse windows directory with space

EvanM
  • 667
  • 1
  • 4
  • 12
  • 1
    FWIW, in your first post, you have a single quote *before* the : which won't work. I'd debug by trying to set your path to just the maven bin directory to avoid parse errors in other parts of your path. – EvanM Aug 31 '17 at 11:32
  • 1
    Also, to debug, you don't need to edit .bashrc, you can change for your current session at the prompt. Also 'source ~/.bashrc' will rerun the script. – EvanM Aug 31 '17 at 11:37
  • 1
    Thx, that are very good hints. But unfortunately it is not working, no matter how I change it. – eztam Aug 31 '17 at 12:23