6

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
  • Is the error from 'source" or from 'myfile'? Try: 'set -v -x; source /home/user/path/to/myfile; set +v +x How about: alias source ; echo $SHELL ; echo $POSIXLY_CORRECT – D McKeon Feb 23 '14 at 19:13
  • I believe my script, when run on the server, is being called like this: sh myscript so even though my script starts with #!/bin/bash and echo $SHELL returns /bin/bash, source is somehow still not working. – MountainX Feb 23 '14 at 20:37
12

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.

  • 1
    Yes! Thank you. I believe my script, when run on the server, is being called like this: sh myscript so even though my script starts with #!/bin/bash that is somehow not enough. So... how do I source a file in this case? Thanks – MountainX Feb 23 '14 at 20:35
  • I checked further. It is somehow using dash (seemingly b/c it is called with sh myscript on Ubuntu). Using the dash dot notation (.) to source works. But is there a way to force my script to run in bash? Bash is available. – MountainX Feb 23 '14 at 20:49
  • 1
    @MountainX, the bang line isn't read by the interpreter, it is read by the kernel when you try to execute the file. – Graeme Feb 23 '14 at 20:50
  • 2
    You have to use bash to run the script or set executable permissions and execute directly. – Graeme Feb 23 '14 at 20:51
  • @MountainX You could symlink sh to bash, but this would likely mess up your system in other ways. Plus you would then be running in POSIX mode and not have all the bash features. – Graeme Feb 23 '14 at 20:53
8

You are probably logging in with a user that runs a shell other than bash. Suppose you log in with user X and X runs sh or another shell, not bash, and with user X you run source myscript.sh, then you should get the given error.

  • Thank you. I believe my script, when run on the server, is being called like this: sh myscript so even though my script starts with #!/bin/bash that is somehow not enough. So... how do I source a file in this case? Thanks – MountainX Feb 23 '14 at 20:36
  • step 1: chmod +x myscript.sh step 2: path/myscript.sh – PersianGulf Feb 23 '14 at 20:39
  • @MountainX If you have no control over what is running your script, you can use the standard name . instead of the bash synonym source. – Gilles Feb 23 '14 at 23:50
  • 1
    @Gilles - that worked. I used .. I didn't know it was the "standard" name and that source was just a synonym for it. – MountainX Feb 24 '14 at 1:20
  • 1
    source actually came from the cshell, and the bourne shell originally used . – X Tian Feb 25 '14 at 22:16
0

I was stack with similar problem when running stuff from rpm.spec file.

I fixed the problem by providing explicit path to the script.

So, instead of

source myfile

I had to to run

source ./myfile

I am not sure what was the exact reason for that but it looks like myfile was searched for on $PATH and in $CWD. I guess that is up to some configuration in shell.

0

Two issues:

  1. Be sure that the script is actually being interpreted by bash.
  2. If the name you use with source does not contain a slash, bash will look for it in $PATH.

The script will not be interpreted by bash if you explicitly run it with e.g. sh script. It will be interpreted by bash if it's executable and its first line is #!/bin/bash (adjust the path to match the location of the bash binary on your system) and you run it with ./script, or if you run it with bash script.

Also, according to the bash manual, source filename will

Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. If filename does not contain a slash, filenames in PATH are used to find the directory containing filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.