Sourcing function file in bash_profile and bashrc but still does not work

advertisements

It works in the interactive shell but not from in a script. This script and the trace that follows demostrates:

set -x
tail -n 2 ../.bash_profile
tail -n 2 ../.bashrc
cat ../FUNC_FILE
FUNC
cat testfunc
testfunc

***14:43:56 502 ~/work>FUNC
imafunc
***14:44:02 503 ~/work>t
++ tail -n 2 ../.bash_profile
. ~/FUNC_FILE
compgen -A function  # List all functions
++ tail -n 2 ../.bashrc
. ~/FUNC_FILE
compgen -A function  # List all functions
++ cat ../FUNC_FILE
function FUNC () { echo imafunc; }
++ FUNC
./t: line 5: FUNC: command not found
++ cat testfunc
FUNC
++ testfunc
./testfunc: line 1: FUNC: command not found


Seems to work for me. Your demonstration doesn't really demonstrate the problem, though. Nowhere in there did you source the scripts you cat'ed.

My working example:

bash$ cat testfunction
function thingie () { echo "Ima function"; }
bash$ cat dofunction
#!/bin/bash
. /path/to/this/directory/testfunction
thingie
bash$ ./dofunction
Ima function

Vs. the non-working example:

bash$ cat dontdofunction
#!/bin/bash
cat /homes/bgerard/personal/tmp/testfunction
thingie
bash$ ./dontdofunction
function thingie () { echo "Ima function"; }
./dontdofunction: line 3: thingie: command not found

Use "." or (equivalently) "source" in place of "cat" and it should work.