z3nth10n
z3nth10n

Reputation: 2461

Back-slashes are disappearing from user input (read -p)

In my script I'm requesting to the user to specify a path. Because I'm on Windows I want to replace all \ to /. This would be a easy task to do, but I'm having some troubles:

read -p "Please, type/paste the working path (folder) you wish to link this scripts: " working_dir

I already did what this answers said:

https://stackoverflow.com/a/6853452/3286975

tr '\\' '/'

https://superuser.com/a/1068082/634144

home_mf="${home//\\//}"

https://stackoverflow.com/a/50220624/3286975

sed 's/\\/\//g'

But I have no luck. This is what I do:

working_dir=$(echo "$working_dir" | <some of the pipe I typed before>)
echo $working_dir

EDIT:

All the quoted text is not useful for the question. I thought that the problem was here. But echoing $working_dir under read -pcommand:

read -p "Please, type/paste the working path (folder) you wish to link this scripts: " working_dir
echo $working_dir

Output this:

...

Why are backslashes disapearing? My logic thinks that B and G should be escaped also, or I'm wrong?

Upvotes: 1

Views: 864

Answers (1)

gniourf_gniourf
gniourf_gniourf

Reputation: 46853

You want to use the -r switch of read to not allow backslashes to escape any characters (as written in help read).

So this will work:

read -rp "Please, type/paste the working path (folder) you wish to link this scripts: " working_dir
working_dir=${working_dir//\\//}

Upvotes: 3

Related Questions