Question

I have a file with some file’s path. Like this:

C:\Users\peter\workspace\etwas.txt

I tried read with this routine

while read LINE
do
  web=$LINE
  echo $web

done <etwas.txt

The result:

C:Userspeterworkspaceetwas.txt

I will read in this form

C:/Users/peter/workspace/etwas.txt

How you can read it?

No correct solution

OTHER TIPS

Try doing this using only bash builtins:

while read -r LINE; do
  web="${LINE//\\//}"
  echo "$web"
done < etwas.txt

output

$ cat etwas.txt 
C:\Users\peter\workspace\etwas.txt
$ while read -r LINE; do
>   web="${LINE//\\//}"
>   echo "$web"
> done < etwas.txt
C:/Users/peter/workspace/etwas.txt

I use bash parameter expansion to substitute \ with /

The most portable way to do this (i.e. not relying on bash extensions) is with the tr command.

tr \\\\ / < etwas.txt | while read LINE
do
    web=$LINE
    echo $web
done

(Quadruple backslash?! Yeah, because both the shell and tr itself treat backslash as an escape character, so you need to write four of them to get a literal backslash in this context. '\\' would also work.)

WARNING: piping input to while may, but then again may not, cause the contents of the loop to be executed in a subshell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top