Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Help
  • Can my PATH include directory names with spaces?

Can my PATH include directory names with spaces?

May 11, 2005 / Dave Taylor / Linux Help, Mac & MacOS Help / 6 Comments

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!

About the Author: Dave Taylor has been involved with the online world since the early days of the Internet. Author of over 20 technical books, he runs the popular AskDaveTaylor.com tech help site. You can also find his gadget reviews on YouTube and chat with him on Twitter as @DaveTaylor.

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
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, Dave, for all your helpful information by buying you a cup of coffee!

6 comments on “Can my PATH include directory names with spaces?”

  1. david says:
    August 2, 2010 at 5:57 pm

    Thanks Thomas!

    Reply
  2. Thomas Steudten says:
    February 11, 2010 at 7:42 am

    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

    Reply
  3. Tony García says:
    April 13, 2007 at 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!

    Reply
  4. newbie says:
    April 3, 2007 at 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..

    Reply
  5. Don Pakey says:
    January 28, 2007 at 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:
    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.

    Reply
  6. Stu Black says:
    October 18, 2005 at 12:01 am

    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

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • How Can I Change or Add Favorites to Apple Maps?
  • Getting Started with WordPad Replacement LibreOffice Writer
  • How to Connect to Hilton Hotel Wi-Fi with a Mac
  • How to Filter and Organize Additional Email Addresses in Gmail
  • How Can I Customize my Android Phone Shortcuts Screen?

On Our YouTube Channel

FiiO FW5 TWS True Wireless Stereo Earphones -- DEMO & REVIEW

Samsung Galaxy Z Flip 5 Android Phone -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Alexa, Kindle, and Nook Help (103)
  • Amazon, eBay, and Online Shopping Help (166)
  • Android Help (241)
  • Apple iPad Help (151)
  • Apple Watch & Smartwatch Help (56)
  • Articles, Tutorials, and Reviews (347)
  • Auto Tech Help (21)
  • Business Advice (201)
  • Chromebook & ChromeOS Help (47)
  • Computer & Internet Basics (797)
  • d) None of the Above (166)
  • Facebook Help (386)
  • Google, Chrome & Gmail Help (198)
  • HTML & Web Page Design (248)
  • Instagram & Threads Help (53)
  • iPhone & iOS Help (643)
  • iPod & MP3 Player Help (173)
  • LinkedIn Help (90)
  • Linux Help (178)
  • Linux Shell Script Programming (90)
  • Mac & MacOS Help (934)
  • Most Popular (16)
  • Outlook & Office 365 Help (41)
  • PayPal Help (68)
  • Pinterest Help (54)
  • Reddit Help (21)
  • SEO & Marketing (82)
  • Spam, Scams & Security (102)
  • Trade Show News & Updates (23)
  • Twitter Help (225)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (980)
  • Wordpress Help (206)
  • Writing and Publishing (72)
  • YouTube Help (47)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (66)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


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 site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2023 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy