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.
~/.bashrcis to be able to call the function. – Nelson Mar 25 '17 at 10:39~/.bashrchas a way of detecting if the shell is running non-interactively ( i.e. when you are running a script ) and will quit without executing your definitions ( that's what sourcing a file does - it "executes" the definitions ). You will need to have a separate file for defining your functions, if you want them to be sourced both from within~/.bashrcAND from a script – Sergiy Kolodyazhnyy Mar 25 '17 at 10:54$HOMEinstead of~but I tested it anyways and it does the same thing – Nelson Mar 25 '17 at 10:55