In addition to the basic patterns * ? and [...] the Bash shell provides advanced pattern matching operators, such as !(pattern-list) ("match all but one of the given patterns"). The extglob shell parameter must be set to use them. Example:
~$ mkdir test ; cd test ; touch file1 file2 file3 ~/test$ echo * file1 file2 file3 ~/test$ shopt -s extglob
If I pass a shell expression to a program that runs it in a sub-shell, the operator throws an error. This runs a test that launches the sub-shell directly (here I run from another directory to make sure that the extension does not happen prematurely):
~/test$ cd .. ~$ bash -c "cd test ; echo *" file1 file2 file3 ~$ bash -c "cd test ; echo !(file2)" # expected output: file1 file3 bash: -c: line 0: syntax error near unexpected token `(' bash: -c: line 0: `cd test ; echo !(file2)'
I tried all kinds of shielding, but nothing that I came up with worked correctly. I also suspected that extglob not installed in the sub-shell, but this is not the case:
~$ bash -c "shopt -s extglob ; cd test ; echo !(file2)" bash: -c: line 0: syntax error near unexpected token `(' bash: -c: line 0: `cd test ; echo !(file2)'
Any solution is appreciated!
source share