Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have made my .bashrc give me two types of prompts;

longp: xanth@X-VboxTux:~➤➤➤ and

shortp:

I have done this by writing a 0 or a 1 to a file and then to determine what prompt should be shown an if else tree is in the PS1 line.

so my question is... Is there a better way to store a state than writing a 0 or a 1 to a file?

code;

bashrcpl=$(<.bashrcpl)
if [ $bashrcpl = "0" ] || [ "$(whoami)" = root ]; then
    if [ "$color_prompt" = yes ]; then
        if [ "$(whoami)" = root ]; then
            PS1='${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u\[\033[0;32m\]@\[\033[0;36m\]\h\[\033[0;32m\]:\[\033[01;34m\]\w\[\033[0;31m\]➤\[\033[1;31m\]➤\[\033[0;32m\]➤\[\033[01;34m\] '
        else
            PS1='${debian_chroot:+($debian_chroot)}\[\033[1;31m\]\u\[\033[0;32m\]@\[\033[0;36m\]\h\[\033[0;32m\]:\[\033[01;34m\]\w\[\033[0;31m\]➤\[\033[1;31m\]➤\[\033[0;32m\]➤\[\033[01;34m\] '
        fi

    else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    fi
else
    PS1='${debian_chroot:+($debian_chroot)}\[\033[0;32m\]➤\[\033[01;34m\] '
fi

# Alias to turn short terminal prompt on or off
alias shortp='echo "1" > .bashrcpl & source ~/.bashrc &> /dev/null'
alias longp='echo "0" > .bashrcpl & source ~/.bashrc &> /dev/null'
share|improve this question

1 Answer 1

How persistent should this setting be?

If you want it to persist across bash restarts and reboots, then you have to commit it to disk, so your file-based solution is not half bad.

If you can live with some default setting (e.g. always start with long prompt, but be able to switch to short one), then you can modify your logic a little: set some variable (e.g $MY_PROMPT_TYPE) to the default value in your .bashrc/.bash_profile, set COMMAND_PROMPT variable (which is evaluated every time bash shows you a prompt) to set PS1 based on the current value of $MY_PROMPT_TYPE variable, and define two aliases to toggle the MY_PROMPT_TYPE.

To satisfy your persistence requirement, you can commit a new default value for PROMPT_TYPE variable to .bashrc/.bash_profile every time you toggle it, similarly to what you're doing already, but without having to re-read .bashrc -- your COMMAND_PROMPT will change the PS1 dynamically for you.

TL;DR: Move your "if/else/fi" to COMMAND_PROMPT, move your .bashrcpl to MY_PROMPT_TYPE in .bashrc/.bash_profile, rewrite toggle aliases.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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