wyonghao
wyonghao

Reputation: 511

git-bash $PATH cannot parse windows directory with space

I would like to use git-bash come with github client on a windows 7 (32bit) machine. Somehow, the git-bash has already learned the $PATH variables from windows system PATH. However, I found the $PATH in git-bash did not parse the win system PATH with space correctly.

for example the $PATH outputs:

"..../c/Program: No such file or directory"

Is there anyway to fix it? where is the git-bash script to construct the $PATH for git shell? Thanks

Upvotes: 41

Views: 72880

Answers (11)

j1k00
j1k00

Reputation: 11

In my case I wanted to set a convenience environment variable pointing inside my OneDrive for Business folder, which was something like /c/Users/username/OneDrive - Business Name/somefolder

So in /c/Users/username/.profile I have a line like

export FOLDER='/c/Users/username/OneDrive - Business Name/somefolder'

Note the single quotes. When I want to use that environment variable I still have to double quote it for it to work, e.g:

$ cd "$FOLDER"

Upvotes: 1

GWD
GWD

Reputation: 1464

I have come to appreciate, specifically if in a Windows OS Git Bash shell "setting", the possibility to use "short names" ie the foldername info you get when you run dir /X in a directory and then use that eg for Program Files this would be PROGRA~1 (which contains no spaces anymore). Then using that shortname in paths or similar definitions within the .bashrc (or similar) files.
As an additional info [here] Program Files (x86) shortname would be PROGRA~2
I recently discovered this to be the most stable solution for exporting a variable in Git Bash's .bash_profile

Upvotes: 0

Mohd Abdul Raheem
Mohd Abdul Raheem

Reputation: 11

Don’t do that, you can try this instead pass the complete path in the ""(quotes). Example$ cd "C:\Users\folder\OneDrive - folder\file"

Upvotes: 1

Lee Smith
Lee Smith

Reputation: 6737

You need to escape non-word-characters with a backslash "\":

For Example:

$ cd Program\ Files\ \(x86\)

Upvotes: 1

Steven Arnold
Steven Arnold

Reputation: 11

Use the bash declare command -

$ declare -x PATH="/path with spaces":$PATH

Upvotes: 1

ilyavaio
ilyavaio

Reputation: 11

Basically to confirm what is working: For environment variables use export keyword:

export JAVA_HOME="/c/Program Files/Java/jdkxxxxx"

same for PATH variable etc.

You can put it in your ~/.bashrc . Then, use variables instead of direct string with spaces

Upvotes: 1

Draco Dynasty
Draco Dynasty

Reputation: 85

The answer for me was close to that of Seagal82. I added the following line at the start of [PATH_TO_GITBASH_INSTALL]\etc\profile:

export HOME="/c/Users/Username\ with\ spaces/"

Then I started a new git bash window and all the paths in $PATH using $HOME got replaced with the anti-slashed space and work correctly.

Upvotes: 2

Seagal82
Seagal82

Reputation: 131

I am having similar problem.
After some struggle, i put this on the first line in my C:\Program Files\Git\etc\profile:

export HOME=`cygpath -u "\`cygpath -d "$HOMEDRIVE$HOMEPATH"\`"`

and type command echo $HOME in bash will come out /c/Users/NAMEXX~1, no more space parsing problem for my $HOME or ~/ !

Upvotes: 2

Ishrak
Ishrak

Reputation: 547

git-bash runs in a Linux environment. As, linux doesn't recognize \ backslash in its directory path, you need to use / forwardslash. Use "" if you directory path contains spaces.

Upvotes: 0

Chris Hermut
Chris Hermut

Reputation: 1758

Why not using absolute path instead of relative path and if a folder of file name contains a space just use "" (quotes).

Ex. cd C:/Users/"My Folder"

Upvotes: 48

Sam Haars
Sam Haars

Reputation: 381

Put a backslash ("\") before the space.

ex. cd My\ Documents

Upvotes: 28

Related Questions