How can I get this script file's functions to load without having to source it every time?
I created a file foo
with script functions I'd like to run. It's in /usr/bin
, which is in the PATH.
File foo:
#!/bin/bash
echo "Running foo"
function x {
echo "x"
}
However, when I type a function name like x
in the terminal:
x: command not found
When I type foo
:
Running foo
shows up (so the file is in PATH and is executable)
After typing source foo
, I can type x
to run function x
A pretty basic question, I know. I'm just trying to abstract my scripts so they're more manageable (compared to dumping everything in .profile or .bashrc).
Best Answer
mikeserv's answer is good for the details of what goes on "behind the scenes", but I feel another answer here is warranted as it doesn't contain a simple usable answer to the exact title question:
The answer is: Source it from your
.bashrc
or your.bash_profile
so it is available in each shell you run.For example, I have the following in my
.bash_profile
: