I have a file with extensions:


I have an array containing a list of extensions:

exts=(sh pl py)

I want to remove the extensions in exts from the filename's. Is there a trick to do it, I'm thinking of something of this kind:


It obviously does not work.

1 Answer 1


I would construct a pattern from your exts array and then use sed :

exts=(sh pl py)

function array_to_regex_alts() { # joins the array elements by \|
    echo -n "$1"; shift; printf "\|%s" "$@"

pattern="\.\($(array_to_regex_alts ${exts[@]} )\)"

echo $f1 | sed "s/$pattern//g"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .