Skip to main content
1 of 3

Bash Parameter Substitution: command line vs. script

I am practicing with parameter substitution on bash.

I wrote the following dummy:

#!/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 the last part of the file name.


The execution of dummy 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 the eval of dummy 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 in confined in the sub-string '?(.random)'. The issue could be with ?, since it is a reserved key in the context of parameter substitution. However, if that was the issue, I would expect the pattern to either fail or succeed the same in both cases.

Where's the probably obvious pitfall?