Results 1 to 5 of 5

Thread: Bash white space in variable storing a filename problem

  1. #1
    Join Date
    May 2008
    Location
    Edmonton, Canada
    Beans
    Hidden!

    Bash white space in variable storing a filename problem

    Sorry I couldn't make the title any less confusing. Basically I was writing a script to automate some backups of files I have when I came across a bit of a problem. The simplified version of the problem is this:

    I'm trying to understand why the following will not work as expected:

    Code:
    $ ls -l /home/user/space\ file 
    -rw-r--r-- 1 user user 0 2010-05-19 17:05 /home/user/space file
    
    $ TEST="/home/user/space file"; ls $TEST
    ls: cannot access /home/user/space: No such file or directory
    ls: cannot access file: No such file or directory
    
    $ TEST="/home/user/space\ file"; ls $TEST
    ls: cannot access /home/user/space\: No such file or directory
    ls: cannot access file: No such file or directory
    
    $ TEST='/home/user/space file'; ls $TEST
    ls: cannot access /home/user/space: No such file or directory
    ls: cannot access file: No such file or directory
    
    $ TEST='/home/user/space\ file'; ls $TEST
    ls: cannot access /home/user/space\: No such file or directory
    ls: cannot access file: No such file or directory
    If you haven't noticed the differences, it's every combination of (with/without escape character) and (single/double quotes). As far as I know, at least one of those should have worked (I would have figured either attempt 2 or 4). Any reason why they are not? This is bash version 4.1.5(1) by the way.

  2. #2
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Bash white space in variable storing a filename problem

    The following will work:

    $ TEST="/home/kaibob/space file" ; ls "$TEST"
    test file
    Last edited by kaibob; May 20th, 2010 at 01:09 AM.

  3. #3
    Join Date
    May 2008
    Location
    Edmonton, Canada
    Beans
    Hidden!

    Re: Bash white space in variable storing a filename problem

    Holy crap you're a genius! Thanks! Still looks kind of weird. How come that works? Why would $TEST be different than "$TEST"?

  4. #4
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Bash white space in variable storing a filename problem

    Quote Originally Posted by terminator14 View Post
    Holy crap you're a genius! Thanks! Still looks kind of weird. How come that works? Why would $TEST be different than "$TEST"?
    The Bash Reference Manual does a good job of explaining this issue:

    http://www.gnu.org/software/bash/man...f.html#Quoting

    This provides a somewhat less technical and perhaps more easily understood explanation:

    http://www.linuxtopia.org/online_boo...ect_03_03.html
    Last edited by kaibob; May 20th, 2010 at 01:23 AM.

  5. #5
    Join Date
    May 2008
    Location
    Edmonton, Canada
    Beans
    Hidden!

    Re: Bash white space in variable storing a filename problem

    Hmm... Now that I think about it, it sounds like it would interpret the variable as the string it represents while the "" would simply keep the two parts of the file name together - the "/home/kaibob/space" part and the "file" part. That sounds right?

    I've been meaning to give the bash manual a read but never got around to it since I had school. Now that I'm done I'll give it a go. Thanks!

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
  •