How do I remove the file suffix and path portion from a path string in Bash?

Here’s how to do it with the # and % operators in Bash.

$ x="/foo/"
$ y=${}
$ echo ${y##*/}

${} could also be ${x%.*} to remove everything after a dot or ${x%%.*} to remove everything after the first dot.


$ x="/foo/"
$ y=${x%.*}
$ echo $y
$ y=${x%%.*}
$ echo $y

Documentation can be found in the Bash manual. Look for ${parameter%word} and ${parameter%%word} trailing portion matching section.

Leave a Comment