Reputation: 625
Im using shell script for some specific purposes and of the functions it has is to relad .bash_profile
:
function refresh {
source "$HOME/.bash_profile"
}
That same .bash_profile
has this statement:
if [ -f "$HOME/.bash_prompt" ]; then
source "$HOME/.bash_prompt"
fi
Which should also reload .bash_prompt
; and it does, that prompt file contains values which should change display of the prompt (colors, text placement and such) but those values don’t change. They change only on new terminal window or if I explicitly call source "$HOME/.bash_prompt"
inside terminal window.
Am I doing something wrong here?
Here is my .bash_prompt
source:
# Colors
# Bunch of color codes
function print_before_the_prompt {
# create a $fill of all screen width
let fillsize=${COLUMNS}
fill=""
while [ "$fillsize" -gt "0" ]
do
fill="-${fill}" # fill with underscores to work on
let fillsize=${fillsize}-1
done
printf "$txtrst$bakwht%s" "$fill"
printf "\n$bldblk%s%s\n" "${PWD/$HOME/~}" "$(__git_ps1 "$txtrst [$txtblu%s$txtrst]")"
}
# Load Git completion and prompt
if [ -f "/usr/local/opt/git/etc/bash_completion.d/git-completion.bash" ]; then
source "/usr/local/opt/git/etc/bash_completion.d/git-completion.bash"
fi
if [ -f "/usr/local/opt/git/etc/bash_completion.d/git-prompt.sh" ]; then
source "/usr/local/opt/git/etc/bash_completion.d/git-prompt.sh"
fi
GIT_PS1_SHOWDIRTYSTATE=true
PROMPT_COMMAND=print_before_the_prompt
PS1="\[$txtred\]⦿\[$txtrst\] "
Upvotes: 1
Views: 2654
Reputation: 2599
You need to also source the script hosting the refresh
function, instead of executing it. If you don't do this, the environment is only changed during the execution of the script.
Explanation: when you execute a script, it inherits its parent's current environment (in this case: your shell) and it's given its own environment. All environment changes in the script will only apply to the script itself and its children.
However, when you source a script, all the changes and commands directly affects the parent's environment.
In general, it's recommended to keep the scripts intended to be sourced separated to general purpose scripts. For example, you can have a dev.sh
file containing special environment variables for a particular development project which needs some special variables.
If you want a quick way to source your .bash_profile
for your current shell, you can set an alias. Doing it by executing a script is impossible.
Upvotes: 5