3

I need to ask the user to provide a path name (which will here have a space) to launch a find command:

read -r -e -p "Folder:" folder
Folder :/mnt/My\ folderX/
echo $folder

gives:

My\ folderX/

But..

find $folder | sort -rn

gives:

find: /mnt/My\: No such file or directory
find: folderX/: No such file or directory

and

find "$folder" | sort -rn

gives:

find: /mnt/My\ folderX/: No such file or directory

Is there something I did wrong? Can't figure out why the second command won't run as it should take it as:

find /mnt/My\ folderX/ | sort -rn

Which works perfectly.

Many thanks

2
  • which shell are you using ?
    – X Tian
    Mar 4, 2017 at 14:25
  • i'm using sh (on mac os x) Mar 4, 2017 at 14:29

1 Answer 1

6

With -r read will prevent the \ from being an escape character, so either don't type the \ or don't use -r.

So try it as follows:

read -rep "Folder: " folder
find "$folder" | sort -rn

and enter /mnt/My folderX without any escapes

or

read -ep "Folder: " folder
find "$folder" | sort -rn

and enter /mnt/My\ folderX (or /mnt/My folderX without the escape--you'll read the whole line into folder so you don't need to escape it either way)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .