If you are using the default Ubuntu ~/.bashrc
file, then extended globbing is enabled by default1 for interactive shells - which is why it works on the command line.
Inside a script however, you will need to enable it with
shopt -s extglob
shopt
is documented in man bash
under the SHELL BUILTIN COMMANDS
section, or via the shell's online help
help shopt
1 albeit rather opaquely, via bash completions. See for example Where is bash's “shopt extglob” turned on for my interactive shell?