Given this Bash code:
TEMP="1_2"
echo ${TEMP/_.*/}
why does it print out 1_2 instead of 1?
I’ve also tried these, but they don’t work:
echo ${TEMP/_\.*/}
echo ${TEMP/_\\.*/}
This does work:
echo ${TEMP/_[0-9]*/}
but I want to know:
- Why isn’t the period acting as a wildcard?
- What should I use instead?
A question mark is the single-character wildcard. However, it doesn’t work like regular expressions where the asterisk is a quantifier. In Bash, in parameter expansions, an asterisk is a multicharacter wildcard.
The following also work in this particular situation. See Parameter Expansion in
man bashfor more information regarding the differences.I recommend against using all-caps variable names in order to reduce the chance of name collision with shell or environment variables.