Bash shave a first and/or last character from string, but only if it is a certain character

bash, regex, sed

Solution

what's the problem with the simple one?

sed "s/^\///;s/\/$//"

Output is

foo/bar/hah
foo/bar/hah

Problem

In bash I need to shave a first and/or last character from string, but only if it is a certain character. If I have | I need ``` /foo/bar/hah/ => foo/bar/hah foo/bar/hah => foo/bar/hah ``` You can downvote me for not listing everything I've tried. But the fact is I've tried at least 35 differents sed strings and bash character stuff, many of which was from stack overflow. I simply cannot get this to happen.

Original source