Issue with copy command

Hi,
I have a shell script which is having copy command. My need is to copy all the files in a directory into its subdirectory.

My directory structure is like this.

Dir1
file1.txt
file2.txt
file3.txt
subDir1
subDir1 is a subdirectory in Dir1.
Now I am trying to copy all the txt files in Dir1 into subDir1 by excluding subDir1. I used the below command.
cp -r !(subDir1) subDir1
When run the above command in terminal, it is working fine but when I used the same command in shell script, it is giving error as below.

syntax error near unexpected token `('

Please suggest.

Stupid question number 1: What shell are you using interactively?

Stupid question number 2: How are you invoking your shell-script?

You are probably using bash interactively; most Linux users are. You may be using sh or /bin/sh to run your script. They are different shells, and have different features. Make sure your script is invoked thus:

bash script

and the first line looks like:

#!/bin/bash

Now, finally, you are using extended globbing in your copy. Extended globbing is an option in bash, and it has to be turned on. Add this line to the top of your script:

shopt -s extglob

and let us know if you are still having problems.

Andrew

Andrew

Thank you for the solution. Answers to your questions..
I am using bash and I will run script as sh. I have already #!/bin/bash in my script file.
cp command worked after I placed 'shopt -s extglob' command on top but can you please tell me is there any impact of this code at any other places? I am new to shell scripting, so trying to understand...