
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? 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
Categorized:
Mac OS X Help
,
Unix and Linux Help
(Article 3993)
Tagged: Previous: Can what someone does off-hours affect my business? Next: How do I learn blogging? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 #find directories and filter. exit 0 Posted by: Stu Black at October 18, 2005 12:01 AMI'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: 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 AMThis 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 PMHi Dave With bash 3.2.48 cd "$x" works: x="ottos dir" For bash too, setting the shell buildin option with Regards I have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Book Links
|