This is pretty strange.
So I developed this thing called SahibLib and it's basically a script that you source that sources a bunch of other scripts.

Its directory tree looks like this:

    |- /home/myuser
       |- /home/myuser/sahiblib
          |- sahibload <- executed in ~/.bashrc
          |- sahiblib <- executed by sahibload
          |- modules
             |- core.sls <- problem file
             |- example.sls <- problem file

What SahibLib is supposed to do is source the .sls files. It doesn't do that though.

It outputs the correct message ("[SUCCESS] Thank you and goodbye."), but the actual files aren't sourced.

myuser@mypc:~$ slhelp
Invalid command.

Strangely, when I source the file manually, it works:

myuser@mypc:~$ . ~/.bashrc
[SUCCESS] Thank you and goodbye.
myuser@mypc:~$ slhelp
SahibLib Core Help

My question is, why isn't the script properly executing in ~/.bashrc?

Update: Writing some debug commands to core.sls, it seems that the file executes perfectly. It gets to the end of the file, and returns a standard exit code. This makes the problem even more strange, because now it seems that Bash is just ignoring the function definitions outright. Also, if you need to see core.sls, or any of the files, they're available here


From your link, near the bottom of that page, I see this line:

echo "/home/myusername/sahiblib/sahibload" >> ~/.bashrc

This indicates that, when ~/.bashrc is run, it will execute sahibload, not source it. That means that any functions defined by sahibload will be lost when execution of sahibload completes and control is returned to ~/.bashrc.

If you want functions defined by sahibload to survive, use:

echo ". /home/myusername/sahiblib/sahibload" >> ~/.bashrc

Simpler example

Let's consider this test file:

$ cat loader
hello() { echo Hello; }

Before executing the script, there is no function hello:

$ hello
bash: hello: command not found

After we execute the script, there still is no function named hello:

$ ./loader
$ hello
bash: hello: command not found

In the above, loader is executed the same way that sahibload is executed in ~/.bashrc.

Now, let's source loader and observe that the definition of hello survives:

$ . ./loader
$ hello

sahibload needs to be sourced, not executed, in ~/.bashrc.

| improve this answer | |
  • 1
    I bow down to thee, Bash overlord. ;) – InitializeSahib Sep 22 '16 at 2:50

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.