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.

7
  • 2
    How do you execute this script? via source in the ~/.bashrc? – Ljm Dullaart Oct 9 '20 at 11:27
  • 1
    You can't change the parent environment (the value of PS1) in a script that is executing in a separate child environment. – Kusalananda Oct 9 '20 at 11:52
  • no, I execute a separate script. I neither source it, nor using commands in ~/.bashrc – Celdor Oct 9 '20 at 11:54
  • 1
    So, what happens if you do . yourscript or source yourscript? (see also the remark by Kusalananda) – Ljm Dullaart Oct 9 '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 9 '20 at 12:26
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.