|
|
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?
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!
More Useful Unix and Linux Help Articles:
✔ Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔ Shell script to convert lowercase to title case?As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔ Can I script renaming files based on an XML data map?I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔ Test for valid numbers in a Bash shell script?In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔ Review: iSSH for the iPad/iPhoneIf you're running an online business like I am, there are times when you need to connect and log in to the server...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help
right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
Categorized:
Mac OS X Help
,
Unix and Linux Help
(Article 3993,
Written by Dave Taylor)
Tagged: Previous: Can what someone does off-hours affect my business? Next: How do I learn blogging? Reader Comments To Date: 6Don Pakey said, on January 28, 2007 12:17 PM:
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: newbie said, on April 3, 2007 10:43 AM:
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.. Tony GarcĂa said, on April 13, 2007 4:00 PM:
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! Thomas Steudten said, on February 11, 2010 7:42 AM:
Hi Dave With bash 3.2.48 cd "$x" works: x="ottos dir" For bash too, setting the shell buildin option with Regards david said, on August 2, 2010 5:57 PM:
Thanks Thomas!
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
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