cd(1) and variables with spaces (cygwin)

2019-07-04 21:26发布

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.

6条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-04 21:31

Due to Word Splitting if you do not quote $PRO that white space breaks your path into multiple words.

查看更多
Ridiculous、
3楼-- · 2019-07-04 21:38

There's no way to use a variable without quoting it in your case.

查看更多
对你真心纯属浪费
4楼-- · 2019-07-04 21:39

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/
查看更多
Luminary・发光体
5楼-- · 2019-07-04 21:42

I got around this with wildcards:

export PRO="/cygdrive/d/Program*Files/"

查看更多
Luminary・发光体
6楼-- · 2019-07-04 21:50

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

查看更多
在下西门庆
7楼-- · 2019-07-04 21:52

Just enclose the program file in double quotes in this way

/cygdrive/c/"Program Files (x86)"

It works for me

查看更多
登录 后发表回答