O problema pode ser que a opção extglob
shell esteja definida no shell interativo, mas não no shell de script.
$ shopt -u extglob
$ echo "${var1/${pattern}/}"
/some/path/to/file/the_file.arbitrary.n.ext.0.random.ext
$ shopt -s extglob
$ echo "${var1/${pattern}/}"
/some/path/to/file/the_file.arbitrary.n.ext
Você pode tentar colocar shopt -s extglob
logo após a linha shebang no script.
Do Manual de referência do bash :
If the
extglob
shell option is enabled using theshopt
builtin, several extended pattern matching operators are recognized.... Composite patterns may be formed using one or more of the following sub-patterns:
?(PATTERN-LIST)
Matches zero or one occurrence of the given patterns.