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.


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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.