The problem is that read
treats backslash in its input as an escape operator (to escape the separators when using read word1 word2
and the newline to allow line continuation).
To do what you want, you need to add the -r
switch to read
which tells it not to do that backslash processing, and you also need to set $IFS
to the empty string so it treats the whole line as the whole word to assign to the variable¹:
IFS= read -rp "Input file name: " FilePath
UnixPath="${FilePath//\\//}"
Additionally, your echo
commands needs double quotes around the variable substitution: echo "$FilePath"
, and anyway in general echo
can't be used to output arbitrary data², so:
printf '%s\n' "$FilePath"
¹ with the default value of $IFS
, to prevent it from stripping leading and trailing blanks (space and tab) from the input line
² and for echo
as well, backslash is or can be treated specially, even in bash
depending on the environment or how it was built.