Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

Can my PATH include directory names with spaces?

Dave, I would like to use an environmental variable to change directories and included files into vim on Mac OS Tiger. I've found that this works:
  export pm="/volumes/Macintosh HD/Documents/pmwiki"
for shell commands, but then I have to use something like: cd "$pm"

Is there a better way which doesn't require the double quotes? Also, is there some interaction with the PATH variable?

Dave's Answer: Ah yes, there's little as frustrating as trying to fully figure out how Unix -- even Mac OS X Tiger -- works with spaces. I had a bit of trouble figuring out exactly what you were asking here, but fortunately we had a bit of back and forth in email and I now see what you're asking.

My answer is in two parts, however. The first question you're asking is actually can I defer evaluation of spaces in a directory name twice? and the second question is can PATH handle directories with spaces in them?.

As far as I can ascertain, there is no easy way to accomplish what you want with the cd command because it's a built-in shell primitive, so its arguments aren't expanded. But using eval to force evaluation makes it work without quotes:

$ mkdir "test me"
$ x="test me"
$ cd $x
-bash: cd: test: No such file or directory
$ x="test\ me"
$ cd $x
-bash: cd: test\: No such file or directory
$ eval cd $x
$ pwd
/Users/taylor/Desktop/test me

I admit, that's not a very satisfying answer! You can't do this in a shell script, either, because you'd change the directory of the subshell running the script, not the parent shell you were using. No good answer, I'm afraid.

In the second case, however, there is a simple solution. Here's a typical approach to tweaking the PATH to add a directory:

PATH=${PATH}:/home/taylor/mybin

To add a directory to the path with spaces, just use a backslash as needed. Here's a proof of concept sequence:

$ cd 'test me'
$ cat > mytestscript
#!/bin/sh

echo this is my test script

exit 0
$ echo $PATH
/bin:/sbin:/usr/bin:/usr/sbin:/sw/bin:/usr/X11R6/bin:/Users/taylor/bin:/sw/bin
$ PATH=${PATH}:"/Users/taylor/Desktop/test me"
$ mytestscript
this is my test script

Hope that helps. If someone has a suggestion on how to solve the former puzzle of a variable that names a directory that contains spaces but that can nonetheless be given to cd, I'd love to hear about it!


Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

I've had the some problem using bash for in, and it breaking up lists with spaces when I didn't want it to. My solution is below, don't know how applicable it is.

#!/bin/bash

SEARCH_DIR=$1
LOOK_FOR=$2
PERMISSION=$3
GROUP=$4

#find directories and filter.
for a in `find "$SEARCH_DIR" -type d | grep $LOOK_FOR | sed 's\ \_-_\g'`
do
#perform chmod/chgrp on 'a'.
a=`echo "$a" | sed -e 's\_-_\ \g'`
chmod -Rv $PERMISSION "$a"
chgrp -Rv $GROUP "$a"
done;

exit 0

Posted by: Stu Black at October 18, 2005 12:01 AM

I'm not sure if this is the same question or just a related question, but I was always having to cd to deeply imbedded directories, with several of the directories in the path having spaces in the names. So I created this very simple .bash_profile file in my home directory, which automatically gets executed at every signon:
CDPATH=${CDPATH}:"~/Documents/Don office/project A/folder A".
Then, if "datafolder" is a folder in "folder A", I can "cd datafolder" to it from anywhere.
Of course, you can also append more pathnames to the CDPATH variable to refer to all of your comonly used directories.
And you can change the PATH variable, as you did in your example, in the .bash_profile file.

Posted by: Don Pakey at January 28, 2007 12:17 PM

i guess u can do this in linux:

to go the folder test me -

/Users/taylor/Desktop/test\ me

test\ then space then me ..will do..

Posted by: newbie at April 3, 2007 10:43 AM

This help me a lot trying to use cd $var in a script shell where $var is a directory structures with a lot of /name like this/directories.

Thanks!

Posted by: Tony GarcĂ­a at April 13, 2007 4:00 PM

Hi Dave

With bash 3.2.48 cd "$x" works:

x="ottos dir"
mkdir "$x"
cd "$x"
pwd

For bash too, setting the shell buildin option with
"shopt -s cdable_vars" you can do:
cd x

Regards
Tom

Posted by: Thomas Steudten at February 11, 2010 7:42 AM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.








Ask Dave Taylor: The iPhone App: Advertisement



Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Linux Journal: Free Issue!

Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2010 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.