How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

In Bash you can do it by enabling the extglob option, like this (replace ls with cp and add the target directory, of course) ~/foobar> shopt extglob extglob off ~/foobar> ls abar afoo bbar bfoo ~/foobar> ls !(b*) -bash: !: event not found ~/foobar> shopt -s extglob # Enables extglob ~/foobar> ls !(b*) abar afoo … Read more

Remove a fixed prefix/suffix from a string in Bash

$ prefix=”hell” $ suffix=”ld” $ string=”hello-world” $ foo=${string#”$prefix”} $ foo=${foo%”$suffix”} $ echo “${foo}” o-wor This is documented in the Shell Parameter Expansion section of the manual: ${parameter#word} ${parameter##word} The word is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching). If the pattern matches the beginning of the … Read more