Artificial intelligent assistant

Checking if string ends with a number throws "unexpected operator" I'm trying to check wether the file name of the script I run ends with a number or not: #!/bin/sh name=$(basename "$0" .sh) [ $name =~ ^.[0-9]$ ] && numb=$(echo $name | sed 's/[^0-9]*//g') || numb=1 echo $numb my shell file is named `mh03.sh` and this is the output if I run it: $ ./mh3.sh ./mh3.sh: 3: [: mh3: unexpected operator 1 can someone tell me why I get this exception and how I can fix it?

The regex match operator `=~` is not supported in the single square brackets. You need double square brackets for it to work.


[[ $name =~ ^.[0-9]$ ]]


Note that you don't need a regex, you can use a normal pattern:


[[ $name = *[0-9] ]]


or, if you need the name to contain something before the digit,


[[ $name = *?[0-9] ]]

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 2b73b4825f7c262ac8dc55e08ad5aa9e