Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
have you executed your code manually , means without bashrc , is it working ?, if possible post your code. –  Rahul Patil Jan 18 '13 at 4:01
add comment

3 Answers

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 Alternative to .bashrc.

share|improve this answer
add comment

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.

share|improve this answer
1  
Or in ~/.profile if it is just one user. –  Johan Jan 18 '13 at 5:59
    
@Johan - You are right, but su specifically indicates it's multiple users. –  jordanm Jan 18 '13 at 14:39
    
I still have problem of setting on /etc/profile, 1. how to separtarte different PS1 on different shell. 2. How to check set PS1 in human interactive only. –  Daniel YC Lin Jan 19 '13 at 8:44
add comment

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}"
share|improve this answer
add comment

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.