Substitution to remove tailing slash?

Heyas

I am trying to remove a tailing space, with substitution.
Already tried some multiple escapes with no luck, is that even possible?

echo $CHROOT
[ -z "$CHROOT" ] || \
	CHROOT="${CHROOT/\/$}"
echo $CHROOT
return

And all i get is:

:) paths $ CHROOT=/usr/local/
+ paths $ . *
/usr/local/
/usr/local/
$SHELL -version
GNU bash, version 4.3.33(1)-release (x86_64-redhat-linux-gnu)

The 2nd printed output, would be expected to be:

/usr/local

Thank you

EDIT:
I can check if the last string is a slash or not, but i thought a subs would work just fine?

Try:

CHROOT=${CHROOT%/}

This strips a trailing slash. Your attempt to use substitution would work, but the pattern isn't regex. You'd use ${CHROOT/%\//}

See relevant section of the bash manual:

1 Like

neutronscott solution is the % (Remove matching suffix pattern) expansion not replace. It happens to product the same result in this case. The replace version should be:

${CHROOT/%\//}

I'll try and highlight the difference here:

$ CHROOT=/test/this/

$ echo ${CHROOT/%\//}
/test/this

$ echo ${CHROOT%/}
/test/this

$ echo ${CHROOT/%\//Z}
/test/thisZ

$ echo ${CHROOT%/Z}
/test/this/

In the 3rd example will replace the last slash with Z
In the 4th example we try and delete /Z from end of string (no match, so nothing deleted).