0

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.

Celdor
  • 129
  • 2
    How do you execute this script? via source in the ~/.bashrc? – Ljm Dullaart Oct 09 '20 at 11:27
  • 2
    You can't change the parent environment (the value of PS1) in a script that is executing in a separate child environment. – Kusalananda Oct 09 '20 at 11:52
  • no, I execute a separate script. I neither source it, nor using commands in ~/.bashrc – Celdor Oct 09 '20 at 11:54
  • 1
    So, what happens if you do . yourscript or source yourscript? (see also the remark by Kusalananda) – Ljm Dullaart Oct 09 '20 at 12:20
  • Nothing happens and yes I did read the comment from Kusalananda. I understood the problem. I think I realise that If I test the VPN connection and change content of PS1 in there, I should see the updated prompt. I hope I've got it right. Thanks – Celdor Oct 09 '20 at 12:26
  • You appear to be evaluating __RESULT outside of the gp_set_prompt function – steeldriver Oct 09 '20 at 13:07
  • Just one more question. Sorry, if it's trivial, I am still quite a beginner when it comes to Linux. I updated ~/.bashrc, which sets $PS1 depending on VPN status. While it works, .bashrc is invoked only once when I open terminal. It updates PS1 only once according to a current status of VPN at start. I can source .bashrc in shell but I was wondering how I can do it in the script. Simple . ~/.bashrc at the end does not work :/ Thanks – Celdor Oct 10 '20 at 12:10
  • Even if you source .bashrc within 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:20
  • 1
    source file/. file do 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

1 Answers1

0

I stuck it in my .bashrc as an alias

...
alias shortprompt="export PS1=\"\\[\\e[1;36m\\]\\W\\[\\e[1m\\] ~> \\[\\e[0m\\]\""
alias longprompt="export PS1=\"\\[\\e[1;36m\]\\\\[$(tput bold)\\\\]\\w> \\[\\e[0m\\]\""
...

Remember to double up on the "\" where an actual "\" is needed.

To use in the same session type:

source ~/.bashrc

This will allow you to switch prompts in the active session.

Zogg
  • 1