0

I'm using dash(sh) or bash or zsh. If possible, I would prefer to put in common place.

I want to put proper PS1 settings when I interactive with shell, so these situation should be considered

  1. login (show PS1)
  2. su
  3. sudo
  4. script (Don't show PS1)

I was put PS1 on .bashrc, but it seems not always workable.

3 Answers3

4

Each shell has its own escape sequences for PS1, so you need to set it separately for each shell. Furthermore, PS1 only makes sense for interactive shells, it isn't used by other programs. So put PS1 in the interactive startup file for your shell:

  • ~/.bashrc for bash
  • ~/.kshrc for ksh
  • ~/.zshrc for zsh

Bash has a quirk: it doesn't load .bashrc in a login shell, it only loads ~/.bash_profile or absent this ~/.profile. To fix this, put the following lines in your ~/.bash_profile:

if [ -e ~/.profile ]; then . ~/.profile; fi
case $- in *i*) . ~/.bashrc;; esac

For more information about shell setup files, see Is there a ".bashrc" equivalent file read by all shells?.

0

One thing you could do is use commonly named vars to define the terminal escapes specific to each shell's interpretation in their own sourced script, then bring it all together at the end in a single prompt:

~/.zshrc

    esc1='SPECIFIC TO ZSH'
    esc2='SPECIFIC TO ZSH'
    . ~/.common_prompt

~/.bashrc

    esc1='SPECIFIC TO BASH'
    esc2='SPECIFIC TO BASH'
    . ~/.common_prompt

~/.dashrc

    esc1='SPECIFIC TO DASH'
    esc2='SPECIFIC TO DASH'
    . ~/.common_prompt

~/.common_prompt

     esc3='COMMONLY INTERPRETED ESCAPE SEQUENCE'
     PROMPT_COMMAND='eval PS1=\"printf %b "$esc1" "$esc2" "$esc3"\"'

And if one shell is likely to do more with the prompt than another, just .dot sourcing .common_prompt file doesn't have to be the end. So if zsh is going to do more processing than dash, for instance - because it will - you just take it from there:

~/.zshrc

    esc1='SPECIFIC TO ZSH'
    esc2='SPECIFIC TO ZSH'
    . ~/.common_prompt
    _more_processing "${PROMPT_COMMAND}"
mikeserv
  • 58,310
  • 1
    Neither dash nor zsh honors $PROMPT_COMMAND. zsh has an analogue in precmd, but dash has none. – bukzor May 04 '16 at 21:23
0

If you want the same prompt for all shells and users, then put it in the /etc/profile file. It is sourced by all shells, and is the standard place for defining PS1.

jordanm
  • 42,678