Global Pattern - exclude directories

All,

I am trying delete folder by adding pattern not to delete certain folders. But i struck with error.

When i use below command from command line, it works fine.

shopt -s extglob
rm -rf !(test1|test2|test3)

But when i use the same in shell script, i get the below error.

syntax error near unexpected token `('

Any help?

Did you set the extglob option in the script as well?

Yes, i did.

---------- Post updated at 09:51 PM ---------- Previous update was at 09:40 PM ----------

And one more information, the script works perfectly in my Mac bash terminal but fails in solaris sun bash terminal.

The default shell on Mac OS is bash; the default shell on SunOS is a Bourne shell. What shell do you specify in the 1st line of you script?

bash for the both.

Just now i got the additional info even it fails in Mac OS. And the below code fails particularly when inside the user function.

#!/bin/bash
SRC_HOME=/u/home/test/src

pack()
{
cd "$SRC_HOME"
	echo "-----Little House keeping-----"
	shopt -s extglob
	rm -rf !(test1|test2|test3)

}
pack

Show us the bash versions on both computers.

I found just now the issue not due to OS shell. But the syntax error arises when we use the below code in user function

shopt -s extglob

	rm -rf !(test1|test2|test3)

---------- Post updated at 10:21 PM ---------- Previous update was at 10:14 PM ----------

Moved the line shopt -s extglob out of the user function and Issue resolved.

Thanks for all responses.