

I have just added a space between if and [ so the shell can see the if command.Īnd the output of the script is correct: (localhost)$. But in reality the shell sees if[ that is not a known command to the shell.Īt 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. If is a shell builtin command and you might be thinking you are using if here.

The error is caused by the missing space between if and the open square bracket ( [ ). unexpected_token.sh: line 5: syntax error near unexpected token `then' I get back the error below: (localhost)$. When I run the following script: #!/bin/bash Lesson 1: Remember to escape Bash special characters when you use them as normal characters (literals) in a filename or string in general.įirst error fixed! Syntax Error Near Unexpected Token Then (Example 1) No errors this time: -rw-r-r- 1 ec2-user ec2-user 28 Jun 28 22:29 report_july.csv I will update the command to include the backslash before both parentheses: mv report\(july\).csv report_july.csv The backslah is used to escape characters. In other words they are special characters.Īnd Bash special character need to be escaped if used as normal characters in a command. When I run it I get the following error: -bash: syntax error near unexpected token `('īecause parentheses () are used in Bash to create a subshell. I can use the following command, right? mv report(july).csv report_july.csv Let’s say I have the following file on my Linux system: -rw-r-r- 1 ec2-user ec2-user 28 Jun 28 22:29 report(july).csvĪnd I want to rename it to report_july.csv. Update your script with the correct line of code.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).Take note of the line mentioned by the Bash error.

