Remove unnecessary slashes from a given path with bash

前端 未结 8 1817
囚心锁ツ
囚心锁ツ 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:12

    1. Consider if you need to do this. On Unix, specifying duplicate path separators (and even things like /foo/.//bar///hello/./world work just fine.
    2. You can use readlink -f, but this will also canonicalize the symlinks in that path, so the result depends on your filesystem and the path supplied must actually exist, so this won't work for virtual paths.

提交回复
热议问题