Coding within the Linux Bash Terminal has grow to be a predominant follow within the coding sector. The software program Engineers and college students studying the coding language come throughout numerous errors. If you’ve repeatedly seen the errors like Syntax error close to sudden token ‘(’ or Bash syntax error close to sudden token, you’ll be able to strive counting on the strategies described within the article and grow to be a proficient coder. Read the strategies described within the article within the order described and repair the errors within the command strains in 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 alternative for the Bourne Shell or sh. The information are named utilizing the .sh format within the Linux Bash scripting. If there are formatting points within the code within the shell script, you could face a syntax error. If the error is near the ( character, the shell will immediate you to the error within the line and can show the error within the corresponding line. Since the Linux Bash is an interpreter, the road with the error will likely be returned to you within the Terminal and it’ll cease scanning the remainder of the instructions within the script. Hence, that you must repair the error within the explicit command line and proceed to the subsequent to repair the sudden token error within the shell script. The causes for the syntax error close to sudden token within the Linux Bash are listed under on this part as under:
- Coding with escape sequences- If you’ve written the codes in Bash scripting, the escape sequences or the citation marks within the script might trigger errors. The escape sequences and citation marks must be written within the appropriate format to repair the error.
- Incorrect syntax within the coding file- The syntax within the code might give the syntax error if the command is written within the improper syntax comparable to altering the order of loops.
- Wrong utilization of the command- If you aren’t utilizing the command accurately comparable to incorrect worth project, you will have a syntax error.
- Incompatible OS within the systems- If the shell for the coding script isn’t suitable between the Unix and DOS programs, you will have an sudden error.
- Issues within the bash shell script- The points operating within the bash shell script on the file copied from one other system might give the sudden token error.
Consider a file named instance.sh created within the Linux Bash scripting with the next command strains for explanatory functions. The instance file has the probabilities for syntax errors and contains all of the doable instructions that can be utilized within the shell script.
str= ‘First command line of ‘(example file)’ within 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; executed whereas true; do if [ $ day == “mon” ]; then echo “mon”; else echo “not mon”; executed; fi
Method 1: Manually Fix Errors in Each Command Line
The first technique to repair the errors is to repair the syntax error manually in every command line within the script. The steps to resolve the syntax errors close to sudden token within the command strains are mentioned on this part. The course of to repair the sudden token error within the Terminal is described under. Run the file within the Terminal by typing the ./instance.sh command and urgent the Enter key.
2. Note the strains which have the sudden token error within the command strains on the outcome under.
3. Fix the error in every line by following the strategies mentioned under individually and saving the file.
4. Run the file once more after the adjustments and test 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 fixing the syntax error within the command strains is to learn the file within the Terminal. IF there are points with the file, you could not be capable to view the file. The common follow of viewing the file is to run the file utilizing the command ./instance.sh however you can’t modify the content material on the file. The choices to view the content material of the file and modify the command strains to repair the Syntax error close to sudden token ‘(’ are mentioned under.
Option 1: Through CAT Command
The first possibility is to make use of the cat command to view the file within the shell script. Read the content material of the file with the sudden token error utilizing the cat command by typing the command cat –v instance.sh within the Terminal.
Note 1: The instance.sh is used for explanatory functions and that you must kind the title of the file that has the sudden token error.
Note 2: The cat –v command is used to show all of the invisible characters that are prone to symbolize the carriage return or no-break house.
Option 2: Through VX Command
If you can’t use the cat command, you’ll be able to strive utilizing the vx command to view and alter the instructions on the file utilizing the step given under. Type the sh –vx ./instance.sh command within the Terminal to open the file.
Option 3: Through od –a Command
3. If there are just a few invisible characters within the command strains, you should utilize the od –a command to view the file. If the content material of the file isn’t seen within the code file, you’ll be able to strive studying the file utilizing the command od –a instance.sh to switch the code.
Step II: Remove Windows Line breaks
If there are Windows line breaks within the shell script, you should utilize the console instructions to take away the road breaks and duplicate the code strains to a brand new file to repair the error.
Enter the next command within the Terminal to avoid wasting the content material within the file to a different file named correctedexample.sh to take away the Windows line breaks within the script.
tr –d ‘r’ <instance.sh> correctedexample.sh
Step III: Set Permissions to Newly Created File
You must set the permission to the newly created file to edit the file in order that the file could be executed within the shell. Type the command as chmod 755 correctedexample.sh within the Terminal to offer the permissions to the file and run the file. You can now view the corrected file and repair the formatting points and repair the Syntax error close to sudden token ‘(’ within the file.
Step IV: Format Code within the File
The second step is to format the code strains individually and alter the command strains manually on the file. The choices to format the file to repair the Syntax error close to sudden token ‘(’ are mentioned under on this part.
Option 1: Replace Single Quotes with Double Quotes
If you’re utilizing the one quotes within the command strains, that you must alter the command by changing the one quote with double quotes to repair the syntax error. In the instance.sh file, take away the code strains which have ‘ and ’ or the single quotes within the command, and substitute the one 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)” within the script”
Note: The double quotes are obligatory for the parameter kind instructions comparable to str= “[(1,2),(3,4)]”.
Also Read: 20 Best Lightweight Linux Distros of 2022
Option 2: Add $ to String Lines
If you’ve added string values to the script, that you must add a $ to the string values to repair the syntax error within the script. Add the $ for the command strains with the string values to repair the sudden error. Here, within the instance file, change the command line as;
str= $ ‘First command line of ‘(example file)’ within the script’
Note: If you’re utilizing the $ within the string worth, you’ll be able to bypass the backslash escape sequence because the command strains are decoded by the ANSI C customary. In different phrases, through the use of a $ for string worth, you’ll be able to keep away from utilizing the double quotes as an alternative of the one quotes within the command strains.
Option 3: Convert Tabs to Spaces
The areas you’ve left between two statements in command must be areas as an alternative of tabs to repair the syntax error within the script. If you’re getting the error on Cygwin, you’ll be able to strive changing the tabs within the codes to areas to repair the error. The command line is given under as;
do echo command1; echo command2; echo command3; echo command4; executed
The above command ought to be rewritten as proven under to repair the error.
do echo command1; echo command2; echo command3; echo command4; executed
Option 4: Use Escape Characters
If you’re utilizing a bash character, it is very important use an escape character together with the bash character to repair the syntax error. The parentheses or () are bash particular characters within the file, therefore, you would want to make use of the escape character or the backslash within the command line to flee the conventional characters to execute the command. The str= ‘First command line of ‘(example file)’ within the script’ command wouldn’t give an error within the Terminal because the escape character are used.
Also Read: How to Get Among Us on Linux
Option 5: Use house between characters
The shell script acknowledges the instructions and statements within the script by the default values. You want to make sure the right utilization of house between characters in order that the shell can establish the command given within the script. The house is a personality that’s used to distinguish two characters within the command line. In the code, there isn’t any house 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 could be solved because the shell bulletin command if is recognized by the shell.
Option 6: Use Command Separator for Statements
The numerous instructions within the shell script have to be separated into statements for the Terminal to establish particular person instructions. You want to make use of a command separator to repair the syntax error within the Linux Bash. The statements within the command have to be separated by a command separator comparable to a semicolon or a ; or a brand new line by urgent the Enter key. For occasion, the command within the code if [ $ day == “mon” ] then needs to be altered as if [ $ day == “mon” ]; then to repair the error. Since the semicolon is used because 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”; executed; fi must be closed within the appropriate order. Altering the code as proven under can repair the sudden token error because the order of the closing the statements is corrected.
whereas true; do if [ $ day == “mon” ]; then echo “mon”; else echo “not mon”; fi; executed
Also Read: What is a Command Line Interpreter?
Method 2: Rewrite Code
If you’ve copied the code and pasted it to a brand new file on the Terminal, you’ll be able to strive rewriting the code manually to repair the error. The errors within the code could be mounted when you have written the code with none format errors within the shell script. This is as a result of the hidden characters and the formatting points within the textual content editor comparable to Microsoft Word, you might need used to repeat and paste the code might have given the error.
Method 3: Use Dos2unix.exe Command
If you’re utilizing a Unix Operating System, you’ll be able to write codes with the road feed character as n to maneuver to the subsequent line within the file. However, in case you are utilizing a Windows OS, that you must transfer to the subsequent line within the code utilizing the carriage return and line feed or the rn within the file. If you execute the code written within the Windows OS within the Cygwin, you could get the Syntax error close to sudden token ‘(’.
To repair the error, that you must clear the carriage return characters utilizing the DOS to Unix command line device as a textual content file format converter. Type the next command as dos2unix.exe instance.sh within the Terminal and you’ll convert the file to the Unix format.
The article has mentioned the essential strategies to repair Bash syntax error close to sudden token ‘(’ within the script. If you’re utilizing a Linux Bash, you should utilize the strategies defined on this part to repair Bash syntax error close to sudden token. If you’ve learn all the article and located the content material to be helpful, please tell us your solutions and queries within the feedback part.