Take a look at BASH_ENV
INVOCATION
When bash is started non-interactively, to run a shell script, for
example, it looks for the variable BASH_ENV in the environment, expands
its value if it appears there, and uses the expanded value as the name
of a file to read and execute. Bash behaves as if the following com‐
mand were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file‐
name.
BASH_ENV
If this parameter is set when bash is executing a shell script,
its value is interpreted as a filename containing commands to
initialize the shell, as in ~/.bashrc. The value of BASH_ENV is
subjected to parameter expansion, command substitution, and
arithmetic expansion before being interpreted as a filename.
PATH is not used to search for the resultant filename.
When bash execute a shell script non-interactively .bashrc is not read and the function file are not sourced.
If BASH_ENV is set to the name of the function file, this file is read and executed like said in INVOCATION.
So no need to source the function file in each shell script which use the function.
Example to illustrate :
Not Ubuntu but debian-linux 4.14.0-3-amd64 #1 SMP Debian 4.14.17-1 (2018-02-14) x86_64 GNU/Linux with Xfce 4.12
Create a file of functions in your $HOME
cat mesfuncbash
piste ()
{
echo $0 >> ~/lapiste
date >> ~/lapiste
}
Create a script which use the function
cat lescript
#!/bin/bash
echo $0
. ~/mesfuncbash
piste
echo fin
here the script source the function file
Create a laucher to lescript on the Desktop
No need of terminal
execute it.
The file ~/lapiste is updated.
Now remove . ~/mesfuncbash from lescript
execute it
The file ~/lapiste is not updated.
If you run lescript on a terminal
Bash tell you :
line 3: piste: command not found
Now you must set BASH_ENV to the name of the function file.
In debian with xfce, .profile is not read at startup so you can't use it to set BASH_ENV.
You must create a file .xsessionrc in your $HOME.
cat .xsessionrc
export BASH_ENV="$HOME/mesfuncbash"
logout and login, this way .xsessionrc is read at startup
Now, you can execute the laucher on the Desktop
and the file ~/lapiste is updated.
$HOME/bin?