问题
I've been having quite an unusual problem. In my .bashrc file, I have set a variable to a path name with spaces in it. I had a feeling this would cause problems, but I played around with setting an alias in a similar way and got it to work like so:
alias npp="\"/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe\""
Now, I thought I could use the same trick for my environment variable -
export PRO="\"/cygdrive/c/Program Files (x86)\""
This worked. Kind of.
[myName]
$ echo $PRO
"/cygdrive/c/Program Files (x86)"
[myName]
$ cd $PRO
bash: cd: "/cygdrive/c/Program: No such file or directory
I've tried placing an escape before the space with and without removing the double quotes, I've tried single quotes with and without the escape. I've tried using grave accents as quotes. I've tried just the escape, I've tried
export PRO=/cygdrive/c/Program\\\ Files\\\ \\\(x86\\\)
None of this has worked. The only thing that has was -
export PRO="/cygdrive/c/Program Files (x86)"
$ cd "$PRO"
Ultimately, I'm trying to find a way to make my variable work without placing quotes around it every single time I type the variable. Having run out of ideas entirely I came here hoping for someone to be able to help me.
回答1:
I got around this with wildcards:
export PRO="/cygdrive/d/Program*Files/"
回答2:
Due to Word Splitting if you do not quote $PRO
that white space breaks your path into multiple words.
回答3:
There's no way to use a variable without quoting it in your case.
回答4:
Technically, I cannot answer your question, but a good workaround is to create a link to the folder you want, create a variable for the link, and then cd to that variable. It's an annoying second step and a pointless link but if its worth putting in your bash profile it might be worth the extra hassel.
Here's what I did:
ln -sf '/cygdrive/c/Users/Mic/Desktop/PENDING - Pics/' '/cygdrive/c/Users/Mic/mypics'
Then I put this in my bash_profile and now I can cd to $pic
pic=/cygdrive/c/Users/Eric/mypics/
回答5:
Just enclose the program file in double quotes in this way
/cygdrive/c/"Program Files (x86)"
It works for me
回答6:
I ran into a similar issue with a shell script evaluating a directory path in a variable without escaping the space in "Program Files". I got around this by running Cygwin as an administrator and creating a symlink. None of the answers here worked.
ln -s "/cygdrive/c/Program Files" /cygdrive/c/ProgramFiles
来源:https://stackoverflow.com/questions/13545351/cd1-and-variables-with-spaces-cygwin