Error message 'source: not found' when running a script

Solution 1:

/bin/sh is not bash. To execute your script with bash, write #!/bin/bash as the first line in your script.

The error source: not found is not dropped, because /etc/profile is not found. It's dropped, because source is not found. source is a Bash built-in function, and you don't execute the script with bash. So it's clear why it is not found. Change the #! line in the script, and it will work.

Solution 2:

Some shells support . instead of source. So you can try something like this

. filename

instead of

source filename

Hope it works

Solution 3:

1. How can I be sure that the script is executed by Bash, even if #!/bin/sh is specified (apparently, it does not guarantee it)?

To be sure that a script written for sh shell (as in your case - see What is difference between #!/bin/sh and #!/bin/bash?) is executed by Bash, just run the following command:

bash script_name

Thus, you will not get that error anymore.

2. Why would it say that these two sources cannot be found when they are unmistakably there?

It doesn't say that those sourced files are not there. It says that the source command is not found. This is normal, because since you start your script with #!/bin/sh line, your script will run using sh and not bash as you may think. Why is it normal? Because source command is a Bash builtin, but not a sh builtin. To source a file in sh, you should use . (dot). Example:

. /etc/profile
. ~/.profile

Another way is to change the shebang line to #!/bin/bash as chaos said in his answer.

Solution 4:

See this SO question:

/bin/sh is usually some other shell trying to mimic The Shell. Many distributions use /bin/bash for sh, it supports source. On Ubuntu, though, /bin/dash is used which does not support source. If you cannot edit the script, try to change the shell which runs it.