In this article, we will see how to solve "xx: command not found" error in Linux Shell Scripting. This is a very common error that most of the beginners face during shell scripting or if someone switched to shell scripting after working with object oriented programming languages like Java, C++, C# etc. It is so because in languages like Java, C++, C# spaces after variable or operator works just fine but the same is treated differently in Linux shell scripting. Here we will try to understand the reason for the error using a real example and how to solve it in case you are also facing the same.
Solved "xx: command not found" error in Linux Shell Scripting
Also Read: How to declare a variable read only in Linux Bash Shell Scripting
There are few reasons of xx: command not found error but here we will see the most common one which result in this error. To better understand the error, we will see below example script which is about calculating the sum of two numbers. If you look from the eye of object oriented programming language programmers then there is no problem in below script but if you look carefully from the eye of a Linux shell scripter then you can notice that there is a space after variable c and after equal(=) operator in Line 8 which makes system think that c is a command rather than a variable which is incorrect.
cyberithub@ubuntu:~$ nano calculate #! /bin/bash a=5 b=7 sum() { c = $(($a+$b)) echo $c } sum()
Output
cyberithub@ubuntu:~$ ./calculate ./calculate: line 8: c: command not found
So to fix the above highlighted error you need to remove the space after variable c as well as after equal(=) operator as shown below.
cyberithub@ubuntu:~$ nano calculate
#! /bin/bash
a=5
b=7
sum()
{
c=$(($a+$b))
echo $c
}
sum
Now if you try to run the script again then you can notice that the script is running fine without any error and showing the desired result.
cyberithub@ubuntu:~$ ./calculate 12
Now you might ask, removing space after variable c makes sense but why to remove space after equal(=) operator ? So, let's understand this as well by using the same script but this time there is only one space after equal(=) operator as shown in the highlighted line below.
cyberithub@ubuntu:~$ nano calculate #! /bin/bash a=5 b=7 sum() { c= $(($a+$b)) echo $c } sum
If you try to run the above script then you will see below error on the output.
cyberithub@ubuntu:~$ ./calculate ./calculate: line 8: 12: command not found
So what's really happened here ? Basically addition worked as expected and the resultant which is equal to 12 got assigned to variable c but since now there is a space after equal(=) operator so system will treat 12 as command. So this is also not allowed in Linux shell scripting. Hence we have to remove space from both after the variable c as well as after the equal(=) operator as discussed above.
To summarize the above problem, in the OOPS based languages like Java, C++, C# etc. spaces are allowed between a variable and an operator or between the variables but in shell scripting system treat spaces differently. So it is important that one needs to be careful about the spaces. Hope this made sense and help you solve command not found error. Please let me know your feedback in the comment box.