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:
$ 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:
$ 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!
Thanks Thomas!
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
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!
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..
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.
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