I am developing a script that will configure and setup an ubuntu-desktop environment. One of the changes it makes is appending functions and other things to the ~/.bashrc file. Later in the script, I need to call one of the functions added to ~/.bashrc but I get the command not found error. Here is an example script:
# t.sh
#!/bin/bash
text='test-func() { echo It works!; }'
echo "$text" >> ~/.bashrc
source ~/.bashrc
test-func
echo checkpoint
Output:
./t.sh: line 10: test-func: command not found
checkpoint
I assumed sourcing ~/.bashrc would update the shell allowing me to call test-func but it does not. Googling around I found exec bash to replace source ~/.bashrc.
New Output:
./t.sh: line 10: test-func: command not found
From my understanding of exec, it just creates a new shell cutting the script off; therefore "checkpoint" is never printed out.
How can I update ~/.bashrc and run the updates in the same script?
Any help is much appreciated.