1

I am trying to run a shell script.

The first two lines are like follow:

#!/bin/bash
gr = (file1 file2 file3)

However, when I run this script, I get the following error: fileName.sh: 2: Syntax error: "(" unexpected

I am not used to writing this type of scripts, but I read that the parentheses are used for grouping...

I don't understand what did I do wrong?

0

2 Answers 2

3

Try doing this to make an array :

gr=(file1 file2 file3)

No spaces allowed in variable assignation in all kinds.

Sign up to request clarification or add additional context in comments.

3 Comments

@bigTree Make sure you're running bash. Try bash script.sh instead of just running it with sh script.sh or ./script.sh.
@konsolebox My bad, now it works thanks! could you tell me what the difference is please? (thanks!)
@bigTree: The #!/bin/bash tells the system to use /bin/bash to execute the script -- but only if you execute it directly by typing its name. If you type sh script-name, you're feeding the script to sh. The whole point of the #! line is that you don't have to do that; you can treat the script as an executable file without having to care (as a user) how it's executed.
2

I've tried your program and I got the same error.

[admin@hp ~]$ sh fileName.sh 
fileName.sh: line 2: syntax error near unexpected token `('
fileName.sh: line 2: `gr = (file1 file2 file3)'

I just change the program like this:

[admin@hp ~]$ cat fileName.sh 
#!/bin/bash
gr=(file1 file2 file3)

And the problem disappear.

[admin@hp ~]$ vi fileName.sh

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.