[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Best way to remove trailing '/' from a path?
From: |
Stephane Chazelas |
Subject: |
Re: [Help-bash] Best way to remove trailing '/' from a path? |
Date: |
Mon, 20 Apr 2015 22:18:10 +0100 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
2015-04-20 14:58:22 -0600, Eric Blake:
> On 04/20/2015 02:20 PM, Stephane Chazelas wrote:
> > 2015-04-10 22:11:37 -0500, Peng Yu:
> >> Hi, ${VAR%%+(/)} has been suggested here. But it is not robust if $VAR
> >> is just a bunch of slashes. Does anybody have a robust yet succinct
> >> implementation? Thanks.
> >>
> >> http://stackoverflow.com/questions/9018723/what-is-the-simplest-way-to-remove-trailing-slashes-from-each-parameter
> > [...]
> >
> > Try:
> >
> > dirname -- "$VAR/."
>
> Won't work on systems where // is a special root. There, 'dirname --
> //' correctly outputs '//', but if $VAR is //, your line would output
> '/'; similarly, if $VAR is /, your line would output '//'.
[...]
True.
dirname -- "$VAR//."
would at least work properly for "/". It would (potentially
incorrectly) report "/" for "//" as well, but I doubt that would
be a problem for the OP.
> And I'm half wondering if the original poster is trying to
> (re-)implement dirname without forking.
Then he could use a shell with the functionality of dirname
built-in like ksh93 (command /opt/ast/bin/dirname) or zsh
(${VAR:h}).
--
Stephane
Re: [Help-bash] Best way to remove trailing '/' from a path?, Stephane Chazelas, 2015/04/20
Re: [Help-bash] Best way to remove trailing '/' from a path?,
Stephane Chazelas <=
Re: [Help-bash] Best way to remove trailing '/' from a path?, Chris F.A. Johnson, 2015/04/20
Re: [Help-bash] Best way to remove trailing '/' from a path?, Eric Blake, 2015/04/21