Coding in the Linux Bash Terminal has become a predominant practice in the coding sector. The software Engineers and students learning the coding language come across various errors. If you have repeatedly seen the errors like Syntax error near unexpected token ‘(’ or Bash syntax error near unexpected token, you can try relying on the methods described in the article and become a proficient coder. Read the methods described in the article in the order described and fix the errors in the command lines on your file.
How to Fix Bash Syntax Error Near Unexpected Token
Linux Bash is a command-line interpreter for the Linux-based system and is a replacement for the Bourne Shell or sh. The files are named using the .sh format in the Linux Bash scripting. If there are formatting issues in the code in the shell script, you may face a syntax error. If the error is close to the ( character, the shell will prompt you to the error in the line and will display the error in the corresponding line. Since the Linux Bash is an interpreter, the line with the error will be returned to you in the Terminal and it will stop scanning the rest of the commands in the script. Hence, you need to fix the error in the particular command line and proceed to the next to fix the unexpected token error in the shell script. The causes for the syntax error near unexpected token in the Linux Bash are listed below in this section as below:
- Coding with escape sequences- If you have written the codes in Bash scripting, the escape sequences or the quotation marks in the script may cause errors. The escape sequences and quotation marks have to be written in the correct format to fix the error.
- Incorrect syntax in the coding file- The syntax in the code may give the syntax error if the command is written in the wrong syntax such as changing the order of loops.
- Wrong usage of the command- If you are not using the command correctly such as incorrect value assignment, you may have a syntax error.
- Incompatible OS in the systems- If the shell for the coding script is not compatible between the Unix and DOS systems, you may have an unexpected error.
- Issues in the bash shell script- The issues running in the bash shell script on the file copied from another system may give the unexpected token error.
Consider a file named example.sh created in the Linux Bash scripting with the following command lines for explanatory purposes. The example file has the possibilities for syntax errors and includes all the possible commands that can be used in the shell script.
str= ‘First command line of ‘(example file)’ in the script’ str= [(1,2),(3,4)] if[ $day == “mon” ] then echo “mon” else echo “no mon” fi for VARIABLE in {0..2}; then do echo command1; echo command2; echo command3; echo command4; done while true; do if [ $ day == “mon” ]; then echo “mon”; else echo “not mon”; done; fi
Method 1: Manually Fix Errors in Each Command Line
The first method to fix the errors is to fix the syntax error manually in each command line in the script. The steps to solve the syntax errors near unexpected token in the command lines are discussed in this section. The process to fix the unexpected token error in the Terminal is described below. Run the file in the Terminal by typing the ./example.sh command and pressing the Enter key.
2. Note the lines that have the unexpected token error in the command lines on the result below.
3. Fix the error in each line by following the methods discussed below individually and saving the file.
4. Run the file again after the changes and check if the syntax error is resolved on the file.
Also Read: How To Install Linux Bash Shell On Windows 10
Step I: Read Content of File
The first step to solving the syntax error in the command lines is to read the file in the Terminal. IF there are issues with the file, you may not be able to view the file. The regular practice of viewing the file is to run the file using the command ./example.sh but you cannot modify the content on the file. The options to view the content of the file and modify the command lines to fix the Syntax error near unexpected token ‘(’ are discussed below.
Option 1: Through CAT Command
The first option is to use the cat command to view the file in the shell script. Read the content of the file with the unexpected token error using the cat command by typing the command cat –v example.sh in the Terminal.
Note 1: The example.sh is used for explanatory purposes and you need to type the name of the file that has the unexpected token error.
Note 2: The cat –v command is used to display all the invisible characters which are likely to represent the carriage return or no-break space.
Option 2: Through VX Command
If you cannot use the cat command, you can try using the vx command to view and alter the commands on the file using the step given below. Type the sh –vx ./example.sh command in the Terminal to open the file.
Option 3: Through od –a Command
3. If there are a few invisible characters in the command lines, you can use the od –a command to view the file. If the content of the file is not visible in the code file, you can try reading the file using the command od –a example.sh to modify the code.
Step II: Remove Windows Line breaks
If there are Windows line breaks in the shell script, you can use the console commands to remove the line breaks and copy the code lines to a new file to fix the error.
Enter the following command in the Terminal to save the content in the file to another file named correctedexample.sh to remove the Windows line breaks in the script.
tr –d ‘\r’ <example.sh> correctedexample.sh
Step III: Set Permissions to Newly Created File
You need to set the permission to the newly created file to edit the file so that the file can be executed in the shell. Type the command as chmod 755 correctedexample.sh in the Terminal to provide the permissions to the file and run the file. You can now view the corrected file and fix the formatting issues and fix the Syntax error near unexpected token ‘(’ in the file.
Step IV: Format Code in the File
The second step is to format the code lines individually and alter the command lines manually on the file. The options to format the file to fix the Syntax error near unexpected token ‘(’ are discussed below in this section.
Option 1: Replace Single Quotes with Double Quotes
If you are using the single quotes in the command lines, you need to alter the command by replacing the single quote with double quotes to fix the syntax error. In the example.sh file, remove the code lines that have ‘ and ’ or the single quotes in the command, and replace the single quotes with the double quotes or the “ and ’’. Here, in the example file, you need to change the code as str= “First command line of “\(example file)\” in the script”
Note: The double quotes are necessary for the parameter type commands such as str= “[(1,2),(3,4)]”.
Also Read: 20 Best Lightweight Linux Distros of 2022
Option 2: Add $ to String Lines
If you have added string values to the script, you need to add a $ to the string values to fix the syntax error in the script. Add the $ for the command lines with the string values to fix the unexpected error. Here, in the example file, change the command line as;
str= $ ‘First command line of ‘\(example file)\’ in the script’
Note: If you are using the $ in the string value, you can bypass the backslash escape sequence as the command lines are decoded by the ANSI C standard. In other words, by using a $ for string value, you can avoid using the double quotes instead of the single quotes in the command lines.
Option 3: Convert Tabs to Spaces
The spaces you have left between two statements in command have to be spaces instead of tabs to fix the syntax error in the script. If you are getting the error on Cygwin, you can try converting the tabs in the codes to spaces to fix the error. The command line is given below as;
do echo command1; echo command2; echo command3; echo command4; done
The above command should be rewritten as shown below to fix the error.
do echo command1; echo command2; echo command3; echo command4; done
Option 4: Use Escape Characters
If you are using a bash character, it is important to use an escape character along with the bash character to fix the syntax error. The parentheses or () are bash special characters in the file, hence, you would need to use the escape character or the backslash in the command line to escape the normal characters to execute the command. The str= ‘First command line of \‘(example file)\’ in the script’ command would not give an error in the Terminal as the escape character are used.
Also Read: How to Get Among Us on Linux
Option 5: Use space between characters
The shell script recognizes the commands and statements in the script by the default values. You need to ensure the proper usage of space between characters so that the shell can identify the command given in the script. The space is a character that is used to differentiate two characters in the command line. In the code, there is no space between the if and [, which gives the unexpected token error as the if[ command is not identified by the shell. If the code is changed to if [ $ day == “mon” ]; then the error can be solved as the shell bulletin command if is identified by the shell.
Option 6: Use Command Separator for Statements
The various commands in the shell script need to be separated into statements for the Terminal to identify individual commands. You need to use a command separator to fix the syntax error in the Linux Bash. The statements in the command need to be separated by a command separator such as a semicolon or a ; or a new line by pressing the Enter key. For instance, the command in the code if [ $ day == “mon” ] then has to be altered as if [ $ day == “mon” ]; then to fix the error. Since the semicolon is used as the command separator between the characters [ and then, you can fix this error.
Option 7: Remove Additional Statements
Sometimes, you may have added additional statements or may have mixed up the codes in case of multiple nested loops. You need to remove the additional statements on the command lines to fix the Syntax error near unexpected token ‘(’ in the Linux Bash. The bash loops for…done or and the constructional constructs if… fi needs to be in the correct syntax. The example file has the wrong syntax in the for loop has the term then which is used in the if statement. Modifying the code as the following code will fix the unexpected token error. The statement then is an additional statement in the code and removing the term will fix the error.
for VARIABLE in {0..2}; do echo command1; echo command2; echo command3; echo command4; done
Option 8: Ensure Order of Closing of Statements is Correct
If you are using many nested or conditional construct statements in the shell script, you have to ensure that the loops are closed in the order they are opened. You can use a new line separator to avoid conflicts with the loops. The order of closing the nested loops and conditional statements should be correct and must not be altered. The loops in the code while true; do if [ $ day == “mon” ]; then echo “mon”; else echo “not mon”; done; fi needs to be closed in the correct order. Altering the code as shown below can fix the unexpected token error as the order of the closing the statements is corrected.
while true; do if [ $ day == “mon” ]; then echo “mon”; else echo “not mon”; fi; done
Also Read: What is a Command Line Interpreter?
Method 2: Rewrite Code
If you have copied the code and pasted it to a new file on the Terminal, you can try rewriting the code manually to fix the error. The errors in the code can be fixed if you have written the code without any format errors in the shell script. This is because the hidden characters and the formatting issues in the text editor such as Microsoft Word, you might have used to copy and paste the code may have given the error.
Method 3: Use Dos2unix.exe Command
If you are using a Unix Operating System, you can write codes with the line feed character as \n to move to the next line in the file. However, if you are using a Windows OS, you need to move to the next line in the code using the carriage return and line feed or the \r\n in the file. If you execute the code written in the Windows OS in the Cygwin, you may get the Syntax error near unexpected token ‘(’.
To fix the error, you need to clear the carriage return characters using the DOS to Unix command line tool as a text file format converter. Type the following command as dos2unix.exe example.sh in the Terminal and you can convert the file to the Unix format.
Recommended:
- How to Create Telegram Account on Android
- Fix Runtime Error C++ on Windows 10
- How to Delete Github Account Permanently
- Top 14 Best Graphics Card for Linux
The article has discussed the basic methods to fix Bash syntax error near unexpected token ‘(’ in the script. If you are using a Linux Bash, you can use the methods explained in this section to fix Bash syntax error near unexpected token. If you have read the entire article and found the content to be useful, please let us know your suggestions and queries in the comments section.