Results 1 to 3 of 3
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 ...
- 10-07-2010 #1Just Joined!
- Join Date
- Oct 2010
- Posts
- 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?
Leon
- 10-07-2010 #2Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
don't use spaces in filenames
the sun is new every day (heraclitus)
- 10-08-2010 #3
This is one of the major pains one has to deal with in automated scripts. However, you could do something like:
I have not found any other/better way to solve such problems. If you stumble upon a solution one day, please share the wisdom.Code:#! bin/bash echo "What directory: " read -e DIR DIR=`echo $DIR | sed 's/ /\\ /g'` cd $DIR
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.


Reply With Quote