This is the mail archive of the mailing list for the cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How create variables to hold directory names on Windows bash?

On Wed, 9 Mar 2005, neal olander wrote:

> I'm working on a PC running Windows XP.  Using win-bash, but not CygWin. I'm a
> long-time Unix programmer, so I'm trying to get bash workin on XP.  But I'm
> having tremendous problems with spaces in folder names.   Finally figured out
> how to "cd" to folders with spaces in names:
> bash> cd "C:\Program Files" # fails

bash> cd 'C:\Program Files'  # works
bash> cd "C:/Program Files"  # works
bash> cd "C:\\Program Files" # works
bash> cd C:\\Program\ Files  # works
bash> cd C:/Program\ Files   # works

Try reading the bash man page on quoting.

> bash> cd "\Program Files" # works

This only works while you're on the C: drive.  Try switching to another
drive, and this will fail.  Also, since this is Cygwin:

bash> cd /cygdrive/c/Program\ Files  # works
bash> cd "/cygdrive/c/Program Files" # works
bash> cd '/cygdrive/c/Program Files' # works

The first line will also get you Tab-completion.

> After typing "Program Files" the 40th time I'm thinking it is time to
> store the string "Program Files" in a string. I tried creating a
> variable and using it:
> declare pf="\Program Files"

declare pf='C:\Program Files'

or, better yet,

declare pf="/cygdrive/c/Program Files"

> cd $pf         # fails due to spaces in variable name

cd "$pf" # succeeds

> QUESTION: In bash on Windows, how can I store directory names (containing
> spaces) in variables so I can use them for "cd"?

If you don't mind using quotes, just use them.  Otherwise, you can try
this stunt:

cd() { builtin cd "$*"; }

in your ~/.bashrc, and then you don't have to quote arguments to "cd".
You'll have the same problem with all of the other programs, though.

BTW, you can have spaces in filenames on Unix too.  In fact, Unix allows
many more characters in filenames than Windows does.  The fact that you
haven't run into this problem just points at how organized Unix package
maintainers are.

> PS:  I"ve tried asking this question on the bash forum, but that is sort of
> dormant and go no replies.  I understand this is a Cygwin mailing list, so I
> started with the "talk" mailing list, figuring I wouldnt bother people too
> much.

Well, this is a list for off-topic blabber about anything tangentially
related to Cygwin.  Your question was on-topic, and thus prime material
for the main Cygwin list.  Given that you posted here anyway, here's some
obligatory off-topic content:

When will people learn to *read the documentation*?  What is it about the
word "documentation" that suggests that it's optional?  Maybe we should
quiz people on the words in particular positions of the bash manual
(preferably towards the end) before we let them ask questions about bash?
Some older games used this to verify that you actually possessed a copy of
the manual (and thus, presumably, bought the game).  Opinions? :-)
      |\      _,,,---,,_
ZZZzz /,`.-'`'    -.  ;-;;,_
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski, Ph.D.
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"The Sun will pass between the Earth and the Moon tonight for a total
Lunar eclipse..." -- WCBS Radio Newsbrief, Oct 27 2004, 12:01 pm EDT

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]