Results 1 to 5 of 5

Thread: Create directory with spaces in name using BASH script

  1. #1
    Join Date
    Dec 2011
    Beans
    13

    Create directory with spaces in name using BASH script

    I'm trying to write a BASH script to rip a cd using cdparanoia, convert it to mp3 using LAME, and then move all the songs into my music directory. However, I've run into a small but immensely frustrating problem which is that I can't seem to create a directory with spaces in the name, for example "~/music/David Bowie - Station to Station".

    Here is what I have right now:
    Code:
     
    echo -n "Enter artist name: "
    read -e ARTIST
    
    echo -n "Enter album name: "
    read -e ALBUM
    
    DIRECTORY_NAME="\"${ARTIST} - ${ALBUM}\""
    
    mkdir -v ~/tmp/$DIRECTORY_NAME
    I've also tried using read without the -e option, but either way what happens is the shell tries to create separate "David", "Bowie", "-", etc. directories.

    The frustrating part is that if I open up an xterm and type mkdir ~/tmp/"David Bowie - Station to Station" I get what I want, but apparently executing that same string from a script doesn't work.

  2. #2

    Re: Create directory with spaces in name using BASH script

    Code:
    mkdir -v ~/tmp/"$DIRECTORY_NAME"
    song="Station to Station"
    Code:
    artist="David Bowie"
    song="Station to Station"
    mkdir "$artist $song"
    
    ls -ld David\ Bowie\ Station\ to\ Station/
    or as SeijiSensei suggests quotes
    Code:
    "/tmp/$DIRECTORY_NAME"
    I believe either method will work, but one might be more zen than the other.
    Last edited by Habitual; April 21st, 2012 at 04:29 PM.
    Windows assumes the user is an idiot.
    Linux demands proof.

  3. #3
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    13,880
    Distro
    Kubuntu Development Release

    Re: Create directory with spaces in name using BASH script

    That's because you enclosed the directory name in quotes when you ran the command from the command line. Do the same thing in your script:

    Code:
    mkdir -v "/tmp/$DIRECTORY_NAME"
    Make sure to use double quotes; single quotes will treat $DIRECTORY_NAME as a literal and not replace it with its contents.

  4. #4
    Join Date
    Dec 2011
    Beans
    13

    Re: Create directory with spaces in name using BASH script

    Thanks guys!

    It works now (yes!), but out of curiosity (a dangerous thing, I know ) my $DIRECTORY_NAME variable did have double quotes around the name, so I'm still confused about why my original approach didn't work.

    If it's value was (literally) "David Bowie - Station to Station" (and it was, that's what got output when I did just 'echo $DIRECTORY_NAME') , then I assumed that in my script ~/tmp/$DIRECTORY_NAME would expand to ~/tmp/"David Bowie - Station to Station". It only has quotes around the last part, but as I noted apparently that was enough for it to work when I typed it into the terminal manually, but in the script it created a directory named "David. Why the difference?

  5. #5
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    13,880
    Distro
    Kubuntu Development Release

    Re: Create directory with spaces in name using BASH script

    Placing the quotes in the string assigned to an environment variable doesn't matter to bash which doesn't actually look at the contents of the variable at all. What matters is whether the string that contains the variable(s) is enclosed in quotes since that's what the parser will act upon.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •