How to avoid 10 common mistakes in shell scripting

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.

Example 1
Example 1 Whitespace while declaration
Solution 1 Removal of whitespace
Solution 1 Removal of whitespace

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.

Example 2 Condition statement without whitespaces
Example 2 Condition statement without whitespaces
Example 2 Condition statement without whitespaces
Example 2 Condition statement without whitespaces
Example 2 Condition statement without whitespaces
Example 2 Condition statement without whitespaces

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

Solution 2 Condition statement with whitespace
Solution 2 Condition statement with whitespace

Scenario 3: For Comparing string and integer use below characters or symbols.

StringInteger
==-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”.

Example 4 String Declaration
Example 4 String Declaration
Error 4 too many arguments
Error 4 too many arguments
Example 4 String Declaration
Solution 4 Printing string with space in between
Solution 4 Printing string with space in between

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.

Example/Solution 5 String concatenation
Example/Solution 5 String concatenation

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.

Example/Solution 6 Output variable value
Example/Solution 6 Output variable value

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

Example/Solution 7 Arithmetic operation
Example/Solution 7 Arithmetic operation

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.

Example/Solution 8 Array declaration
Example/Solution 8 Array declaration

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.

Example/Scenario 9 Changing permission of file
Example/Scenario 9 Changing permission of file

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.

Example 10 Scope of variable
Example 10 Scope of variable
Solution 10 Result of operation
Solution 10 Result of operation
Example 10 Scope of variable
Example 10 Scope of variable
Solution 10 Result of operation
Solution 10 Result of operation

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

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s