Bash script error: source: not found

bashshell-script

I must be missing a fundamental understanding about sourcing files in bash. I've tried the different approaches that seem like they should work, but I still get this error source: not found.

In my script I tried to cd to the directory where the file that I want to source is located before sourcing it. That didn't work.

cd /home/user/path/to/
source myfile

Neither did providing the full absolute path to the file to be sourced:

source /home/user/path/to/myfile

The error is "source: not found" with the line number of the above statement.

Is there something else, something basic, I could be overlooking? I have checked the paths I'm using and I don't see any errors. This problem is repeatable.

I'm running Ubuntu on a Linode server and my scripts all start with:

#!/bin/bash

Best Answer

Your second attempt using the absolute path should be the correct method.

Possible causes of your bug:

  1. The file doesn't exist.
  2. The file exists, but for some reason you can't read it (eg permissions or some filesystem error.
  3. You have an alias which is overriding the builtin source (fix with unalias source)
  4. You have a function which is overriding source (fix with unset -f source)
  5. You are somehow not using bash (although your bang line would suggest you are). source is not POSIX. Using source on dash does not work, only . works.
  6. My test with source with bash in POSIX mode worked, though maybe this is due to my version or compilation flags. Maybe this is different for you and you are in POSIX mode.

1 and 5 give errors like the one you posted.

Related Question