How can I get rid of unnecessary slashes in a given path?
Example:
p="/foo//////bar///hello/////world"
I want:
p="/foo/bar/hello/world"
For a path to a local file on a windows machine, use backslash. For a path to a web resource or file located on a UNIX based machine (includes Macs, Linux), use a slash. The reason .
You need to escape the / as \/ . The escape ( \ ) preceding a character tells the shell to interpret that character literally. Show activity on this post. Escape it !
Thats a replacement pattern using bash parameter expansion. In ${f// /_} : The double slashes // are for replacing all occurrences of space with _ , if you put one slash / , only first space is going to be replaced. The space is there because you are replacing space (with underscore)
Use readlink:
p=$(readlink -m "/foo//////bar///hello/////world")
Notice that this will canonicalize symbolic links. If that's not what you want, use sed:
p=$(echo "/foo//////bar///hello/////world" | sed s#//*#/#g)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With