Run 1 syntax error unexpected

run 1 syntax error unexpected

www.unix.com › unix-for-dummies-questions-and-answers › 3609-syntax-. When I copy the code snippet on my Ubuntu machine, there isn't an error, meaning that the error is not caused by a Bash syntax error. because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID.

Run 1 syntax error unexpected - senseless

Have you ever seen the message “syntax error near unexpected token” while running one of your Bash scripts?

In this guide I will show you why this error occurs and how to fix it.

Why the Bash unexpected token syntax error occurs?

As the error suggests this is a Bash syntax error, in other words it reports bad syntax somewhere in your script or command. There are many things that can go wrong in a Bash script and cause this error. Some common causes are missing spaces next to commands and lack of escaping for characters that have a special meaning for the Bash shell.

Finding the syntax error reported when you execute your script is not always easy. This process often requires you to change and retest your script multiple times.

To make your life easier I have analysed different scenarios in which this syntax error can occur. For every scenario I will show you the script or command with the error and the fix you need to apply to solve the problem.

Let’s get started!

One Approach to Fix Them All

Considering that this syntax error can occur in multiple scenarios you might not be able to find your exact error in the list below.

Don’t worry about it, what matters is for you to learn the right approach to identify what’s causing the error and knowing how to fix it.

And going through the examples below you will learn how to do that.

In some of the examples I will show you how to fix this error if it happens while executing a single command in a Bash shell.

In other examples we will look at Bash scripts that when executed fail with the “unexpected token” error.

To fix the error in a single command it’s usually enough to add or remove some incorrect characters that cause the syntax error in the command.

Knowing how to fix the error in a script can take a bit more time, and for that I will use the following 5-step process:

  1. Run the script that contains the syntax error.
  2. Take note of the line mentioned by the Bash error.
  3. Execute the line with the error in a Bash shell to find the error fast (without having to change the script and rerun it multiple times).
  4. Update your script with the correct line of code.
  5. Confirm the script works.

Makes sense?

It’s time for the first scenario.

Syntax Error Near Unexpected Token ‘(‘

Let’s say I have the following file on my Linux system:

And I want to rename it to report_july.csv.

I can use the following command, right?

When I run it I get the following error:

But, why?

Because parentheses () are used in Bash to create a subshell. In other words they are special characters.

And Bash special character need to be escaped if used as normal characters in a command. The backslah is used to escape characters.

I will update the command to include the backslash before both parentheses:

No errors this time:

Lesson 1: Remember to escape Bash special characters when you use them as normal characters (literals) in a filename or string in general.

First error fixed!

Syntax Error Near Unexpected Token Then (Example 1)

And here is the second scenario.

When I run the following script:

I get back the error below:

Can you see why?

The error is caused by the missing space between if and the open square bracket ( [ ).

And the reason is the following:

if is a shell builtin command and you might be thinking you are using if here. But in reality the shell sees if[ that is not a known command to the shell.

At that point the shell doesn’t know how to handle then given that it hasn’t found if before, and it stops the script with the error above.

The correct script is:

I have just added a space between if and [ so the shell can see the if command.

And the output of the script is correct:

Lesson 2: Spaces are important in Bash to help the shell identify every command.

Syntax Error Near Unexpected Token Then (Example 2)

While writing Bash scripts, especially at the beginning, it’s common to do errors like the one below:

When you run this one-liner here’s what you get:

Let’s find out why…

The syntax of a for loop in Bash is:

And using a single line:

So, as you can see the semicolon is used in Bash to separate commands when you want to write them on a single line.

The reason why the semicolons were not required in the first version of the script is that the newline is a command separator too.

Now, let’s go back to our error…

The one-liner that was failing with an error contains the then statement that as you can see is not part of the structure of a for loop.

The error is telling us:

  • There is a syntax error.
  • The token ‘then‘ is unexpected.

Let’s confirm the one-liner runs well after removing then:

All good!

Lesson 3: When you see a syntax error verify that you are using Bash loops or conditional constructs in the right way and you are not adding any statements that shouldn’t be there.

Syntax Error Near Unexpected Token Done

I have created a simple script in which an if statement is nested inside a while loop. It’s a very common thing to do in Bash.

This script might seem ok, but when I run it I get the following…

Why?

The done and fi statements are correctly used to close the while loop and the if conditional statement. But they are used in the wrong order!

The if statement is nested into the while loop so we should be closing the if statement first, using fi. And after that we can close the while loop using done.

Let’s try the script:

All good now.

Lesson 4: Nested loops and conditional statements need to be closed in the same order in which they are opened.

Syntax Error Near Unexpected Token fi

Let’s look at another scenario in which this syntax error can occur with the fi token:

And this is what I get when I run it:

In this case the Bash shell identifies the if statement and because of that it expects then after it.

As you can see then is there, so what’s the problem?

There is no command separator between the [ ] command (yes….it’s a command) and the then statement.

So, what’s the fix?

Add a command separator immediately after the closing square bracket. We will use the semicolon ( ; ) as command separator.

Our script becomes:

And if I run it I get the correct output:

Lesson 5: Remember to specify command separators in your Bash scripts. Either the semicolon or the newline.

Conclusion

You now have what you need to understand what causes this syntax error in your scripts. You can apply the 5 lessons I have explained in this guide to find a fix.

Take the time to review the lessons at the end of each section so they become part of your Bash knowledge.

If you have any questions please feel free to write them in the comments below.

Now, let’s say you have saved your Bash script using Windows.

And when you run it in Linux you are seeing a syntax error that you can’t really explain because the script looks correct to you.

You might be having the problem explained in this article.

Enjoy your scripting!


Related FREE Course: Decipher Bash Scripting

Claudio Sabato

I’m a Tech Lead, Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!

UNIX for Dummies Questions & Answers

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error: `]' unexpected

I am getting this error Syntax error: `]' unexpected. Did I do something wrong with elif? Does ksh not like double brackets? if ]; then #echo hi source ~/.bashrc; elif ]; then #echo hi source ~/.kshrc; fi (5 Replies)

Discussion started by: cokedude

5 Replies

2. Shell Programming and Scripting

Syntax error `(' unexpected

I have written this in my script but while running i am getting syntax error `(' unexpected. unload to "$BACKUP_DIR/n_fac_fid-$clliname" select * from n_fac_fid where fac_accesskey in (select fac_accesskey From n_fac_ap_fid where ap_clli="$clliname"); Any help appreciated. (3 Replies)

Discussion started by: suryanmi

3 Replies

3. Shell Programming and Scripting

Syntax error: 'fi' unexpected

unzip file.zip if ] ; then echo "Success" else echo "Some failure." fi ; I tried many time to detect the unzip error, but it keep show the syntax error wherever how I change the syntac. Hope someone can help me fix the issue, thanks. Please use code tags next time for your code and... (5 Replies)

Discussion started by: duncanyy

5 Replies

4. Shell Programming and Scripting

Syntax error near unexpected token '('

I tried to execute the code but I got this error ./Array.c: line 9: syntax error near unexpected token '(' ./Array.c: line 9: ' nvals = get_data(a,MAXARRAY);' and #include<stdio.h> #define MAXARRAY 1000 main() { int a, nvals; nvals =... (7 Replies)

Discussion started by: sgradywhite

7 Replies

6. Shell Programming and Scripting

syntax error: `$' unexpected

Hi all, Am very new to Unix and am currently Involved in Migrating some Shell Scripts from AIX 4 to Solaris 10. While using teh for loop am getting the below error: $ echo $SHELL /usr/bin/ksh $ for file in $(ls *SEBE*) syntax error: `$' unexpected while the same works without issue on... (4 Replies)

Discussion started by: paragkhanore

4 Replies

7. Shell Programming and Scripting

Syntax error near unexpected token `('

Guys , This is an output of my script errored out for "Syntax error near unexpected token `(' " Can someone tell me whats wrong with my script. Below is my original script pasted. #!/bin/bash Script Creation Date 01/21/2010 Author baraghun ... (7 Replies)

Discussion started by: raghunsi

7 Replies

8. Shell Programming and Scripting

unexpected syntax error

Hi, i am getting following syntax error ...kingly advice why is it coming ?? #!/bin/bash find . -name "common.log" if ; then echo "1" fi [email protected] /bin $ ./a.sh ./a.sh: line 7: syntax error near unexpected token `fi' ./a.sh: line 7: `fi' (9 Replies)

Discussion started by: himvat

9 Replies

9. Shell Programming and Scripting

sh: syntax error: `...' unexpected???

Hello all, I want to create a script that polls every hour a directory for the existence of a file. The file I look for is a `token` dropped by an external process at the completion of a successful FTP process. I wrote this script `checkfile.ksh`: #!/usr/bin/ksh if ] then mailx... (5 Replies)

Discussion started by: alan

5 Replies

10. Shell Programming and Scripting

Syntax Error: 'else' is not expected

We have a ksh script to insert data into our data warehouse. There are multiple if/then/else statements, most are not embedded. In this example, there is an if/then statement, and an embedded if/then/else/fi statement. The problem occurs with the closing else. I receive an error message as... (6 Replies)

Discussion started by: djschmitt

6 Replies

All times are GMT -4. The time now is 09:30 AM.

Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.

Privacy Policy

SyntaxError: Unexpected token

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

Message

SyntaxError: expected expression, got "x" SyntaxError: expected property name, got "x" SyntaxError: expected target, got "x" SyntaxError: expected rest argument name, got "x" SyntaxError: expected closing parenthesis, got "x" SyntaxError: expected '=>' after argument list, got "x"

Error type

What went wrong?

A specific language construct was expected, but something else was provided. This might be a simple typo.

Examples

Expression expected

For example, when chaining expressions, trailing commas are not allowed.

Correct would be omitting the comma or adding another expression:

Not enough brackets

Sometimes, you leave out brackets around statements:

The brackets may look correct at first, but note how the is outside the brackets. Correct would be putting brackets around the :

See also

/bin/sh: 1: Syntax error: "(" unexpected ??

My buggy OS is "at work" .
Periodically it get an entry in Qt editor which I did not make , especially when doing "cut and paste". My build process usually finds the extras, but this time it is in make somewhere...

I do not know how to access the make file.

I can successfully build and run a project .
After few simple edit another build gets me this :

I need a SUGGESTION to find and correct this error.
Can I access makefilke "/bin/sh " using QtCreator IDE ?

I prefer not to monkey with anything attached to "make / qmake" - when things works Qt tIDE DOES THE job (for me ) just fine.

or when I "Run qmake " get this OK output

I honestly do not know how I got this output :

PS
I try to exit and reload the project and SAME error persist.
Once the "(" unexpected is there it won't go away - it needs to be manually deleted is my best guess.


Sorry, that: Run 1 syntax error unexpected

Boot error usb ubuntu
Failed flashing process error getting subscriber
Bot error 0x73
run 1 syntax error unexpected

Similar video

SyntaxError : Unexpected token in JSON at position 0 - Fix Unexpected token in JSON error

UNIX for Dummies Questions & Answers

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error: `]' unexpected

I am getting this error Syntax error: `]' unexpected, run 1 syntax error unexpected. Did I do something wrong with elif? Does ksh not like double brackets? if ]; then #echo hi source ~/.bashrc; elif ]; then #echo hi source ~/.kshrc; fi (5 Replies)

Discussion started by: cokedude

5 Replies

2. Shell Programming and Scripting

Syntax error `(' unexpected

I have written this in my script but while running i am getting syntax error `(' unexpected. unload to "$BACKUP_DIR/n_fac_fid-$clliname" select * from n_fac_fid where fac_accesskey in (select fac_accesskey From n_fac_ap_fid where ap_clli="$clliname"); autocad fatal error unhandled e0434f4dh Any help appreciated. (3 Replies)

Discussion started by: suryanmi

3 Replies

3. Shell Programming and Scripting

Syntax error: 'fi' unexpected

unzip file.zip if ] ; then echo "Success" else echo "Some failure." fi ; I tried many time to detect the unzip error, run 1 syntax error unexpected, but it keep show the syntax error wherever how I change the syntac. Hope someone can help me fix the issue, thanks. Please use code tags next time for your code and. (5 Replies)

Discussion started by: duncanyy

5 Replies

4. Shell Programming and Scripting

Syntax error near unexpected token '('

I tried to execute the code but I got this error ./Array.c: line 9: syntax error near unexpected token '(' ./Array.c: line 9: ' nvals = get_data(a,MAXARRAY);' and #include<stdio.h> #define MAXARRAY 1000 main() { int a, nvals; run 1 syntax error unexpected =. (7 Replies)

Discussion started by: sgradywhite

7 Replies

6. Shell Programming and Scripting

syntax error: `$' unexpected

Hi all, Am very new to Unix and am currently Involved in Migrating some Shell Scripts from AIX 4 to Solaris 10. While using teh for loop am getting the below error: $ echo $SHELL /usr/bin/ksh $ for file in $(ls *SEBE*) syntax error: `$' unexpected while the same works without issue on. (4 Replies)

Discussion started by: paragkhanore

4 Replies

7. Shell Programming and Scripting

Syntax error near unexpected token `('

Guys This is an output of my script errored out for "Syntax error near unexpected token `(' " Can someone tell me whats wrong with my script. Below is my original script pasted. #!/bin/bash Script Creation Date 01/21/2010 Author baraghun . (7 Replies)

Discussion started by: raghunsi

7 Replies

8. Shell Programming and Scripting

unexpected syntax error

Hi, i am getting following syntax error .kingly advice why is it coming ?? #!/bin/bash find. -name "common.log" if ; then echo "1" fi [email protected] /bin $ ./a.sh ./a.sh: line 7: syntax error near unexpected token `fi' ./a.sh: line 7: `fi' (9 Replies)

Discussion started by: himvat

9 Replies

9. Shell Programming and Scripting

sh: syntax error: `.' debian read only file system error all, I want to create a script that polls every hour a directory for the existence of a file. The file I look for is a `token` dropped by an external process at the completion of a successful FTP process. I wrote this script `checkfile.ksh`: #!/usr/bin/ksh if ] then mailx. (5 Replies)

Discussion started by: alan

5 Replies

10. Shell Programming and Scripting

Syntax Error: 'else' is not expected

We have a ksh script to insert data into our data warehouse, run 1 syntax error unexpected. There are multiple if/then/else statements, most are not embedded. In this example, there is an if/then statement, and an embedded if/then/else/fi statement. The problem occurs with the closing else. I receive an error message as. (6 Replies)

Discussion started by: djschmitt

6 Replies

All times are GMT -4. The time now run 1 syntax error unexpected 09:30 AM.

Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.

Privacy Policy

/bin/sh: 1: Syntax error: "(" unexpected ??

My buggy OS is "at work" .
Periodically it get an entry in Qt editor which I did not makeespecially when doing "cut and paste". My build process usually finds the extras, but this time it is in make somewhere.

I do not know how to access the make file.

I can successfully build and run a project .
After few simple edit another build gets me this :

I need a SUGGESTION to find and correct this error.
Can I access makefilke "/bin/sh " using QtCreator IDE ?

I prefer not to monkey with run 1 syntax error unexpected attached to "make / qmake" - when things works Qt tIDE DOES THE job (for me ) just fine.

or when I "Run qmake " get this OK output

I honestly do not know how I got this output :

PS
I try to exit and reload the project and SAME error persist.
Once the "(" unexpected is there it won't go away - it needs to be manually deleted is my best guess.


SyntaxError: Unexpected token

The JavaScript exceptions "unexpected run 1 syntax error unexpected occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

Message

SyntaxError: expected expression, got "x" SyntaxError: expected property name, got "x" SyntaxError: expected target, got "x" SyntaxError: expected rest argument name, got "x" SyntaxError: expected closing parenthesis, got "x" Propagating raise_application_error to unix expected '=>' after argument list, got "x"

Error type

What went wrong?

A specific language construct was expected, but something else was provided. This might be a simple typo.

Examples

Expression expected

For example, when chaining expressions, trailing commas are not run 1 syntax error unexpected would be omitting the comma or adding another expression:

Not enough brackets

Sometimes, run 1 syntax error unexpected, you leave out brackets around statements:

The brackets may look correct at first, but note how the is outside the brackets. Correct would be putting brackets around the :

See also

Have run 1 syntax error unexpected ever seen the message “syntax error near unexpected token” while running one of your Bash scripts?

In this guide I will show you why this error occurs and how to fix it.

Why the Bash unexpected token syntax error occurs?

As the error suggests this is a Bash syntax error, in other words it reports bad syntax somewhere in your script or command. There are many things that can go wrong in a Bash script and cause this error. Some common causes are missing spaces next to commands and run 1 syntax error unexpected of escaping for characters that have a special meaning for the Bash shell.

Finding the syntax error reported when you execute your script is not always easy. This process often requires you to change and retest your script multiple times.

To make your life easier I have analysed different scenarios in which this syntax error can occur. For every scenario I will show you the script or command with the error and the fix you need to apply to solve the problem.

Let’s get started!

One Approach to Fix Them All

Considering that this syntax error can occur in multiple scenarios you might not be able to find your exact error in the list below.

Don’t worry about it, what matters is for you to learn the right approach to identify what’s causing the error and knowing how to fix it.

And going through the examples below you will learn how to do that.

In some of the examples I will show you how to fix this error if it happens while executing a single command in a Bash shell.

In other examples we will look at Bash scripts that when executed fail with the “unexpected token” error.

To fix the error in a single command it’s usually enough to add or remove some incorrect characters that cause the syntax error in the command.

Knowing how to fix the error in a script can take a bit more time, and for that I will use the following 5-step process:

  1. Run the script that contains the syntax error.
  2. Take note of the line mentioned by the Bash error.
  3. Execute the line with the error in a Bash shell to find the error fast (without having to change the script and rerun it multiple times).
  4. Update your script with the correct line of code.
  5. Confirm the script works.

Makes sense?

It’s time for the first scenario.

Syntax Error Near Unexpected Token ‘(‘

Let’s say I have the following file on my Linux system:

And I want to rename it to report_july.csv.

I can use the following command, right?

When I run it I get the following error:

But, why?

Because parentheses () are used in Bash to create a subshell, run 1 syntax error unexpected. In other words they are special characters.

And Bash special character need to be escaped if used as normal characters in a command. The backslah is used to escape characters.

I will update the command to include the backslash before both parentheses:

No errors this time:

Lesson 1: Remember to escape Bash special characters when you use them as normal characters (literals) in a filename or string in general.

First error fixed!

Syntax Error Near Unexpected Token Then (Example 1)

And here is the second scenario.

When I run the ole error 800 referstorange excel script:

I get back the error below:

Can you see why?

The error is caused by the missing space between if and the open square bracket ( [ ).

And the reason is the following:

if is a shell builtin command and you might be thinking you are using if here. But in reality the shell sees if[ that is not a known command to the shell.

At that point the shell doesn’t know how to handle then given that it hasn’t found if before, and it stops the script with the error above.

The correct script is:

I have just added a space between if and [ so the shell can see the if command.

And the output of the script is correct:

Lesson 2: Spaces are important in Bash to help the shell identify every command.

Syntax Error Near Unexpected Token Then (Example 2)

While writing Bash scripts, run 1 syntax error unexpected, especially at the beginning, run 1 syntax error unexpected, it’s common to do errors like the one below:

When you run this one-liner here’s what you get:

Let’s find out why…

The syntax of a for loop in Bash is:

And using a single line:

So, as you can see the semicolon is used in Bash to separate commands when you want to write them on a single line.

The reason why the semicolons were not required in the first version of the script is that the newline is a command separator too.

Now, let’s go back to our error…

The one-liner that was failing with an error contains the then statement that as you can see is not part of the structure of a for loop.

The error is telling us:

  • There is a syntax error.
  • The token ‘then‘ is unexpected.

Let’s confirm the one-liner runs well after removing then:

All good!

Lesson 3: When you see a syntax error verify that you are using Bash loops or conditional constructs in the right way and you are not adding any statements that shouldn’t be there.

Syntax Error Near Unexpected Token Done

I have created a simple script in which an if statement is nested inside a while loop. It’s a very common thing to do in Bash.

This script might seem ok, but when I run it I get the following…

Why?

The done and fi statements are correctly used to close the while loop and the if conditional statement. But they are used in the wrong order!

The if statement is nested into shadowlands terrorist 1995 while loop so we should be closing the if statement first, using fi. And after that we can close the while loop using done.

Let’s try the script:

All good now.

Lesson 4: Nested loops and conditional statements need to be closed in the same order in which they are opened.

Syntax Error Near Unexpected Token fi

Let’s look at another scenario in which this syntax error can occur with the fi token:

And this is what I get when I run it:

In this case the Bash shell identifies the if statement and because of that it expects then after it.

As you can see then is there, run 1 syntax error unexpected, so what’s the problem?

There is no command separator between the [ ] command (yes….it’s a command) and the then statement.

So, what’s the fix?

Add a command separator immediately after the closing square bracket. We will use the semicolon ( ; ) as command separator.

Our script becomes:

And if I run it I get oracle raise application error correct output:

Lesson 5: Remember to specify command separators in your Bash scripts. Either the semicolon or the newline.

Conclusion

You now have what you need to understand what causes this syntax error in your scripts. You can apply the 5 lessons I have explained in this guide to find a fix.

Take the time to review the lessons at the end of each section so they become part of your Bash knowledge.

If you have any questions please feel free to write them in the comments below.

Now, let’s say you have saved your Bash script using Windows.

And when you run it in Linux you are seeing a syntax error that you can’t really explain because the script looks correct to you.

You might be having the problem explained in this article.

Enjoy your scripting!


Related FREE Course: Decipher Bash Scripting

Claudio Sabato

I’m a Tech Lead, Software Engineer and Programming Coach, run 1 syntax error unexpected. I want to help you in your journey to become a Super Developer!

0 Comments

Leave a Comment