You are not logged in.

#1 2009-04-14 18:12:09

alleluia20
Member
Registered: 2009-04-13
Posts: 53

How to change the bash prompt?

When I open a shell, I get as prompt:

user@myhost:~$

Maybe it comes from my former Debian installation (I kept /home when installing Arch), because:

$ echo $PS1
${debian_chroot:+($debian_chroot)}\u@\h:\w\$

When I su to root, I get:

bash-3.2#

What do I have to do in order to get something like:

root@myhost:/home/user#

Browsing the Internet, I found that it has to do with /etc/profiles, but I did not manage to find out the specific steps.

Thank you.

Offline

#2 2009-04-14 18:27:51

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: How to change the bash prompt?

you can simply change the PS1 in your bashrc. Remember though, that if you change it in /home/$USER/.bashrc, it will change only the prompt for your user.

I just change my user's and i use the default one for root.

EDIT: Look for a thread titled "Post your PS1" to see what others use and you will get a bunch of different ones to choose from. I stole mine from there as well big_smile

Last edited by Inxsible (2009-04-14 18:28:40)


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#3 2009-04-14 18:39:57

test1000
Member
Registered: 2005-04-03
Posts: 834

Re: How to change the bash prompt?

from man bash:

PROMPTING
       When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it  needs
       more input to complete a command.  Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters
       that are decoded as follows:
              \a     an ASCII bell character (07)
              \d     the date in "Weekday Month Date" format (e.g., "Tue May 26")
              \D{format}
                     the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a  locale-specific
                     time representation.  The braces are required
              \e     an ASCII escape character (033)
              \h     the hostname up to the first `.'
              \H     the hostname
              \j     the number of jobs currently managed by the shell
              \l     the basename of the shell's terminal device name
              \n     newline
              \r     carriage return
              \s     the name of the shell, the basename of $0 (the portion following the final slash)
              \t     the current time in 24-hour HH:MM:SS format
              \T     the current time in 12-hour HH:MM:SS format
              \@     the current time in 12-hour am/pm format
              \A     the current time in 24-hour HH:MM format
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash, version + patch level (e.g., 2.00.0)
              \w     the current working directory, with $HOME abbreviated with a tilde
              \W     the basename of the current working directory, with $HOME abbreviated with a tilde
              \!     the history number of this command
              \#     the command number of this command
              \$     if the effective UID is 0, a #, otherwise a $
              \nnn   the character corresponding to the octal number nnn
              \\     a backslash
              \[     begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
              \]     end a sequence of non-printing characters

       The  command  number  and the history number are usually different: the history number of a command is its position in the history list, which may
       include commands restored from the history file (see HISTORY below), while the command number is the position in the sequence of commands executed
       during  the  current  shell session.  After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expan‐
       sion, and quote removal, subject to the value of the promptvars shell option (see the description of the shopt command under  SHELL  BUILTIN  COM‐
       MANDS below).

if you don't understand what it means just start by typing in one and one prompt and see how it looks afterwards. for example:

PS1='linuxroxxors \d'

then save it to your /etc/profile(export PS1=...) if you want all users to see this prompt(root user too) or your ~/.bashrc if only you want this user to see it.

Last edited by test1000 (2009-04-14 18:43:19)


KISS = "It can scarcely be denied that the supreme goal of all theory is to make the irreducible basic elements as simple and as few as possible without having to surrender the adequate representation of a single datum of experience." - Albert Einstein

Offline

#4 2009-04-14 20:56:07

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: How to change the bash prompt?

You need to put your PS1 definitions for root into /root/.bashrc

/etc/profile, /etc/profile.bash and the files below /etc/profile.d are meant for global initializations.


To know or not to know ...
... the questions remain forever.

Offline

#5 2009-04-15 01:53:43

alleluia20
Member
Registered: 2009-04-13
Posts: 53

Re: How to change the bash prompt?

Thank you. Then, I do not understand the behaviour, because /etc/profile.bash has the PS1 that I want, and the file /root/.bashrc does not exist. Shouldn't bash take the PS1 in /etc/profile.bash when /root/.bashrc does not exist?

My /etc/profile.bash

#
# /etc/profile.bash
# Global settings for bash shells
#

PS1='[\u@\h \W]\$ '
PS2='> '
PS3='> '
PS4='+ '

export PS1 PS2 PS3 PS4

#In the future we may want to add more ulimit entries here,
# in the offchance that /etc/security/limits.conf is skipped
ulimit -Sc 0 #Don't create core files

if test "$TERM" = "xterm" -o \
        "$TERM" = "xterm-color" -o \
        "$TERM" = "xterm-256color" -o \
        "$TERM" = "rxvt" -o \
        "$TERM" = "rxvt-unicode" -o \
        "$TERM" = "xterm-xfree86"; then
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'
    export PROMPT_COMMAND
fi

Thank you very much in advance.

Offline

#6 2009-04-15 08:01:22

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: How to change the bash prompt?

Just tried it out (again). If I removed my /root.bashrc, the PS1 settings from /etc/profile.bash will be used.

One more thought:
Which command do you use to you enter root? It should be "su -", not "su" only. The dash forces bash to load the proper root environment and thus the wanted PS1 definitions (/etc/profile.bash or /root/.bashrc resp.) If you used "su" only, bash will enable root access in the current user environment.

If I remember right, then the prompt format "bash-3.2" does indicate that bash did not find any PS1 definition at all (not even the default ones from /etc/profile.bash). (Edit: This is nonsense. Just tried to unset PS1 which causes the prompt to completely disappear. But there was some default bash prompt of this kind. I only don't remember the exact circumstances.)

So you should check whether your default environments were properly loaded at all. E.g. what does the "set" command tell when you are in root?

Last edited by bernarcher (2009-04-15 08:24:26)


To know or not to know ...
... the questions remain forever.

Offline

#7 2009-04-15 18:15:36

alleluia20
Member
Registered: 2009-04-13
Posts: 53

Re: How to change the bash prompt?

Well, I renamed .bashrc (in order to keep it as a backup) in my home folder and then I got the bash-3.2$ prompt. For some reason, the PS1 in /etc/profile.bash is not taken.

If I just su, I got bash-3.2#. If I "su -", I got [root@myhost ~]#. Hypothesis: /etc/profile.bash is only observed by the root environment. May this be right?

So I edited the .bashrc that Debian had left, and I had to place it both as /home/user/.bashrc and /root/.bashrc. I copy it at the end. See that I made the prompt coloured when being root.

Still, if I did "su -" or "su username -", or logged-in in text-mode as root, I got the profile that is on /etc/profile.bash hmm So I renamed /etc/profile.bash and copied that same file /root/.bashrc as /etc/profile.bash. I hope not to have committed any insanity smile , otherwise please let me know.

Thank you.

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
export HISTCONTROL=ignoredups

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"

## set variable identifying the chroot you work in (used in the prompt below)
#if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
#    debian_chroot=$(cat /etc/debian_chroot)
#fi

# set a fancy prompt (non-color, unless we know we "want" color)
#case "$TERM" in
#xterm-color)
#    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
#    ;;
#*)
#    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
#    ;;
#esac

# Comment in the above and uncomment this below for a color prompt
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

## If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
#    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
#    ;;
#*)
#    ;;
#esac

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

#if [ -f ~/.bash_aliases ]; then
#    . ~/.bash_aliases
#fi

# If id command returns zero, you've root access.
if [ $(id -u) -eq 0 ];
then # you are root, set colour prompt
# see codes for colours on http://bash-hackers.org/wiki/doku.php/scripting/terminalcodes
  PS1="\\[$(tput setaf 5)\\][\\u@\\h:\\w]# \\[$(tput sgr0)\\]"
else # normal
  PS1="[\\u@\\h:\\w]$ "
fi

# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
    eval "`dircolors -b`"
    alias ls='ls --color=auto'
    #alias dir='ls --color=auto --format=vertical'
    #alias vdir='ls --color=auto --format=long'
fi

# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

Offline

#8 2009-05-02 06:59:32

deej
Member
Registered: 2008-02-08
Posts: 395

Re: How to change the bash prompt?

I don't know if this will help, but my set-up gives a different prompt for:
Normal user, user who has "su" 'd and user who has "su -" 'd.

Your normal users prompt goes in ~/.bashrc.

The prompt the user gets after issuing "su" goes into /root/.bashrc
[ Create  the file if it doesn't exist ].

The prompt the user gets after issuing "su -" goes into /etc/profile.bash.

Obviously, the prompts are tailored accordingly.

Hope this helps...

Deej

Offline

#9 2009-05-03 19:23:52

alleluia20
Member
Registered: 2009-04-13
Posts: 53

Re: How to change the bash prompt?

Of course this helps! It ultimately clarifies things. Thank you!

Offline

#10 2009-05-04 05:48:29

deej
Member
Registered: 2008-02-08
Posts: 395

Re: How to change the bash prompt?

You're welcome smile

Deej

Offline

Board footer

Powered by FluxBB