Skip to main content
Tweeted twitter.com/StackUnix/status/819035089313169410

I am practicing with parameter substitution on bashin bash.

I wrote the following dummydummy 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 patternpattern is meant to stripstrip off the last part of the file name.


The execution of dummyExecuting 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 the eval of dummyevaling the script's contents or, equivalently, the direct input of that sequence of commands in the interactive shellinteractive 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 patternpattern '.[0-9]*.ext' works, so the issue clearly is confined into the sub-string '?(.random)'. The issue could be with ?, since it is a reserved keyreserved character in the context of parameter substitutionparameter substitution. However, if that waswere the issue, I would expect the patternpattern to either fail or succeed the same in both cases.

Where's the probably obviousprobably obvious pitfall?

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 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?

I am practicing with parameter substitution 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 evaling 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?

deleted 3 characters in body
Source Link

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?

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?

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 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?

Source Link

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?