Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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


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?


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\] '
            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\] '

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

# 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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .