Ensuring / at end of directory path

I have a variable name storing a directory path

set dirpath = /home/chrisd/tatsh/branches/terr0.50/darwin

I want to ensure there is always a "/" at the end, and if there are more than one "/", that I reduce it to just one.

---------- Post updated at 08:16 AM ---------- Previous update was at 08:13 AM ----------

I have decided to change this, so ensuring there is not any "/" at the end. If there is a one at the end I want to remove it.

Try this, will remove any no. / in end

echo "$dirpath" | sed 's/[\/]*$//g'

Rksiva's solution above is far more sensible, messing with Bash substitutions recently and went a little bit overboard here :wink:

dirpath=/etc///
echo $dirpath
/etc///
while [ ${dirpath:$(( ${#dirpath} - 1 ))}  == '/' ] ; do dirpath=${dirpath:0:$(( ${#dirpath} - 1 ))} ;done
echo $dirpath
/etc

Assuming you're using some (t)csh variant:

while ( "$dirpath" =~ "*/" ) 
  set dirpath = "$dirpath:h"
end

Consider the following though: Csh Programming Considered Harmful.