Remove unnecessary slashes from a given path with bash

前端 未结 8 1794
囚心锁ツ
囚心锁ツ 2021-02-05 15:04

How can I get rid of unnecessary slashes in a given path?

Example:

p=\"/foo//////bar///hello/////world\"

I want:

p=\"/f         


        
相关标签:
8条回答
  • 2021-02-05 15:28

    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)
    
    0 讨论(0)
  • 2021-02-05 15:38

    your input:

    p="/foo//////bar///hello/////world"
    

    command to remove the irrelevant slashes:

    echo $p | tr -s /
    

    output:

    /foo/bar/hello/world
    
    0 讨论(0)
提交回复
热议问题