I am practicing with [parameter substitution][1] in bash.

I wrote the following dummy script:

 #!/bin/bash

 var1="/some/path/to/file/the_file.arbitrary.n.ext.0.random.ext"
 var2="/some/path/to/file/the_file.arbitrary.n.ext.0.ext"

 pattern='.[0-9]?(.random).ext'

 echo "${pattern}"
 echo "${var1/${pattern}/}"
 echo "${var2/${pattern}/}"

Basically, the pattern is meant to strip off the last part of the file
name. 

---

Executing the dummy script results in:

 ~$ ./dummy.sh 
 .[0-9]?(.random).ext
 /some/path/to/file/the_file.arbitrary.n.ext.0.random.ext
 /some/path/to/file/the_file.arbitrary.n.ext.0.ext

whereas `eval`ing the script's contents or, equivalently, the direct input of that sequence of commands in the interactive shell, results in:

 ~$ eval "$(cat dummy.sh)"
 .[0-9]?(.random).ext
 /some/path/to/file/the_file.arbitrary.n.ext
 /some/path/to/file/the_file.arbitrary.n.ext

The pattern `'.[0-9]*.ext'` works, so the issue clearly is confined to the sub-string `'?(.random)'`. The issue could be with `?`, since it is a reserved character in the context of parameter substitution. However, if that were the issue, I would expect the pattern to either fail or succeed the same in both cases.

Where's the probably obvious pitfall?


 [1]: http://www.tldp.org/LDP/abs/html/parameter-substitution.html