Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm using read to read a path from a user like so:

read -p "Input the file name: " FilePath

The user now enters this string:

\Supernova\projects\ui\nebula

What can I do to replace \ with /. The result I want is:

/Supernova/projects/ui/nebula 

By the way, the command:

echo $FilePath

outputs the result:

Supernovaprojectsuinebula

I have no idea what's wrong with it.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

The problem is that read will try to evaluate backslash escapes entered by the user. To do what you want, you need to add the -r switch to read which tells it to leave backslash escapes un-evaluated:

read -rp "Input file name: " FilePath
UnixPath="${FilePath//\\//}"

Additionally, your echo commands needs double quotes around the variable substitution: echo "$FilePath"

share|improve this answer
    
@ Joseph R. - I get it that echo '\Supernova\projects\ui\nebula' get result is right:/Supernova/projects/ui/nebula. But is it possable to unuse quotes to get: /Supernova/projects/ui/nebula ? Because when I must do this: read -p "Input the file name : " FilePath then: echo $FilePath,but get Supernovaprojectsuinebula in this way. So I can't use single quotes. –  binghenzq Nov 6 '13 at 14:34
    
@binghenzq Please see the updated answer. –  Joseph R. Nov 6 '13 at 15:01
    
@binghenzq Also, if you don't mind, I took the liberty of editing your question with this new information because I believe it makes the problem clearer. You can always roll back to your earlier version if you find my edits inaccurate. –  Joseph R. Nov 6 '13 at 15:05
    
@ Joseph R. - Extremely grateful,It is a Perfect answer. –  binghenzq Nov 7 '13 at 4:51
    
@binghenzq Thanks. I'm glad I could help :) –  Joseph R. Nov 7 '13 at 22:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.