Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    BASH script read deletes "\ " special character in directory name

    I am building a script that asks the user for a directory, cds to that directory, and recursively ls the entire directory tree.

    #! bin/bash
    echo "What directory: "
    read -e DIR
    cd $DIR

    The problem is when I give the scritpt a starting directory of /home/user/my\ name
    it complains about /home/user/my directory doesn't exist.

    The read is not saving the "\ " special character.

    How do I get around this?


  2. #2
    Linux User
    Join Date
    Jan 2007
    don't use spaces in filenames
    the sun is new every day (heraclitus)

  3. #3
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    This is one of the major pains one has to deal with in automated scripts. However, you could do something like:

    #! bin/bash
    echo "What directory: "
    read -e DIR
    DIR=`echo $DIR | sed 's/ /\\ /g'`
    cd $DIR
    I have not found any other/better way to solve such problems. If you stumble upon a solution one day, please share the wisdom. Please note also, that the above works only if the input is strictly unescaped. If parts of it contains already escaped spaces, the above will double escape the already escaped spaces, which in fact will escape the backslash!
    Last edited by Kloschüssel; 10-08-2010 at 08:56 AM.

  4. $spacer_open

Posting Permissions

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