Many times when we are working on Linux we have come across a pretty common error regarding source. This error is source: command not found, and today we are going to explain to you everything that you need to know about it.
In this article, you will learn what is source, why this error happens and of course, you will also learn how to solve it, so if you are getting the error source commando not found on your Linux system please keep reading.
First off, what is the source command exactly? It is important to state that source is not an actual Linux software package, it is not a proper program as, for example, du, egrep, find, etc. Source is a built-in command, it is part of the bash software package.
To understand what it does, first, we need to know what shell variables and environment variables are on Linux. Every time we open a new SSH session on a Linux system the shell automatically loads some default variables. These variables are fine for most tasks, but for others, we may need to make some changes, and that is where the source command enters the stage.
If we want to change some variables to, for example, run a script, then the best way to do that is to use source. Most of the time when we run a script on Linux it will run in a subshell, and thus will not run in the same environment as our current shell session. Well, source will solve that for use if we need to use some custom variables.
First, we will need to define a variable in our current shell environment. Once that is done, we can use source to alter the environment of the script we run and make it be our current environment, which will make the new variable available for the script we are running.
This can be very useful in some situations, we can do even more with source, though that is not the point of this article. So, if we do something wrong while using source we may get errors, and one of them will be Source command not found, of course, so let’s see what causes this error.
Causes of Source command not found
We will usually get this error as “source: command not found” or, if we are using sudo, as “sudo: source: command not found”, in both cases the problem is that the shell session is expecting us to run a program, but remember that source is not a program, we are actually running a bash built-in like previously explained.
Since the terminal thinks that we are trying to run a program called “source”, it will throw the not found error.
This error also happens if we try to run a program that is not installed on our system, for example, if we try to run something that does not exist, like “somethingsomething”, we get the same error:
[user@localhost ~]$ somethingsomething bash: somethingsomething: command not found
So, how can we solve this then? That is what we are going to explain now.
How can I fix the Source command not found error on Linux?
The best and easiest way to solve this problem is to run the command using root instead, so we will need to start a root session using su or sudo -s. If you get an authentication error or a permissions error then it means you can’t log in as root with your current user, please ask the person in charge to change the privileges so you can temporarily login as root to solve the problem.
Once you are logged in as root try to run the command that returned the source error before: it should run fine this time.
Another way to fix this is to force the use of bash. In some Linux distros, the source built-in is not available by default because the sh command is not calling bash, but other programs like “dash” for example. So, in this case, we can edit the script we are trying to run and change the /bin/sh at the top to /bin/bash, like this:
A third solution, though it does not always work, is to use a dot instead of source. Yes, it works the same way, so if we are running a script like this:
We will have to replace source with a dot. Yes, a dot:
It is very important to have a space between the two dots, just like in the example.
So that was pretty simple as you saw. The real problem with the source command not found error is to understand why it happens, and now you know that it happens because the shell session is mistaking source for a program when it actually is a Bash built-in.
Once we understand that, solving the problem is easy, in fact, we have given you three solutions that you can try, which are running the script as root, modifying the script to force it to run bash, or using a dot instead of source in the command line.