I would like to update the bash prompt inside a script. Essentially, I have to use a VPN to remotely work on another computer and would like it to get reflected by the bash prompt. Unfortunately whatever I change in PS1 is not update in the bash-shell.
A part of my script that changes PS1 is the following:
# ...
__RESULT=${__RESULT,,}
# CONDITION: set prompt
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
case $__RESULT in
*enabled* | *enable* | *connected* | *connect*)
PS1='(gp on) \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
;;
esac
export PS1
However, it's not working. I found another question, which suggests setting PROMPT_COMMAND. I have followed the suggestions but it is neither working. Also, I am afraid this variable might already be used by another application and I wouldn't really like to overwrite it. So the second version:
__RESULT=${__RESULT,,}
# CONDITION: set prompt
function gp_set_prompt {
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
case $__RESULT in
*enabled* | *enable* | *connected* | *connect*)
PS1='(gp on) \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
;;
esac
export PS1
set_conda
}
export PROMPT_COMMAND=gp_set_prompt
At this point, I am not quite sure what else I can do. Any help? Thanks.
sourcein the~/.bashrc? – Ljm Dullaart Oct 09 '20 at 11:27PS1) in a script that is executing in a separate child environment. – Kusalananda Oct 09 '20 at 11:52~/.bashrc– Celdor Oct 09 '20 at 11:54. yourscriptorsource yourscript? (see also the remark by Kusalananda) – Ljm Dullaart Oct 09 '20 at 12:20__RESULToutside of thegp_set_promptfunction – steeldriver Oct 09 '20 at 13:07~/.bashrc, which sets$PS1depending on VPN status. While it works,.bashrcis invoked only once when I open terminal. It updatesPS1only once according to a current status of VPN at start. I can source.bashrcin shell but I was wondering how I can do it in the script. Simple. ~/.bashrcat the end does not work :/ Thanks – Celdor Oct 10 '20 at 12:10.bashrcwithin a script, execution of said script would not work as you seem to intend it. I know this is confusing (it's been for me for quite long (still can be sometimes)). I'll try to explain, in bash variable are valuated for a specific context (a scope). When you run a script it gets it's own scope with it's own variables and values. while you can pass down variable down to a child script (through export for instance) there is no way to "modify up" (as in change a parent's variable). – Ar3s Jan 30 '24 at 10:20source file/. filedo work because it does not execute the file as a child but rather reads said file in your current context as if you'd have executed it by hand. sourcing in a script would update variable in the context of the script (which is the child of the scope you are manipulating, hence you won't see difference on your scope no matter how many files you source in a script). This rather counter-intuitive (at least to me) mechanic frustrated me a lot, I hope I was able to help you avoid that pain ;) – Ar3s Jan 30 '24 at 10:22