In this post, we will see 10 common syntactical mistakes that we do while writing a shell script. The prerequisite for this post is an EC2 instance for Linux OS on AWS, putty, or GitBash tool and knowledge of shell scripting. Below scenarios are common in shell scripting.
Scenario 1: While defining a variable we should not have whitespace. Define the variable without any whitespace in between.


Scenario 2: When we check a condition in If statement, do include spaces while validating the condition. If everything is written in one line then we will get the error. Avoid mistakes as shown in the below screenshots.



We have to separate each character when we do the comparison. As shown in the below screenshot.

Scenario 3: For Comparing string and integer use below characters or symbols.
String | Integer |
== | -eq |
<= | -le |
>= | -ge |
< | -lt |
> | -gt |
!= | -ne |
Scenario 4: When there is a long string assigned to a variable and we compare that variable with a particular, then it will give an error if we write $varname, instead, we have to write as “$varname”.




Scenario 5: In some case when we do string concatenation to a variable, then system consider it as variable $var_string and results in it into blank value. To avoid this we should put the variable name as ${var}_string.

Scenario 6:Let’s assume that when we want to output some result we should put it in the double quote “” e.g echo “$var”, if we put in a single quote then it will print the result as is.

Scenario 7: We cannot perform arithmetic operations directly through echo command. We have to use expr or $(()).

Scenario 8: When we are using array and want to display array data then if we do $arr then it will give the first element of the array, rather we have to use ${arr[*]} to display all records.

Scenario 9: When we create a script using the VIM command and try to execute it, it will give us an error. Once we create a script, we have to give permission to the script through the “chmod” command and try executing it.

Scenario 10: Scope of variable in a shell script is global, if we declare a variable in function and access that variable outside the function or manipulate that variable outside function then its value will change. Hence, declare a variable as local while using it in function.




This is how we can prevent errors while writing shell script.