Search

Setting up the Bash Prompt

The usual prompt on any terminal in the Linux systems is
user@computer-name:directory$


But if you don't like it you are actually free to change it and it is really simple to change it. The prompt is saved in a variable "PS1". To change the prompt you just have to change this prompt.

Open a terminal and save your current prompt in a temporary variable.
original=PS1

Now you can go ahead and change it to what ever you like
for eg
PS1="Hello \u $" 


This will change the prompt to
Hello username $
if the username is test then it would be
Hello test $

The "\u" is an escape sequence offered by bash, which returns the username of the current user.
There are a number of such escape sequence offered by bash.


\a     an ASCII bell character (07)
              \d     the date  in  "Weekday  Month  Date"  format
                     (e.g., "Tue May 26")
              \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
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash,  version  +  patchlevel
                     (e.g., 2.00.0)
              \w     the current working directory
              \W     the  basename  of the current working direc­
                     tory
              \!     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 con­
                     trol sequence into the prompt
              \]     end a sequence of non-printing characters



Any of the above can be used to change the prompt.

To set the prompt to what is was before
PS1=$original 


The above method will change the prompt temporarily, only for that session, to make this change permanent we need to set the value of PS1 in the file ~/.bashrc so that every time the bash is launched its displays the same prompt.

1 comment:

  1. There is any conection between ASCII and 1337? Just askin'..and you have an award to pick_up from my blog.

    PS_ did you recive my lesson 2? Hope you understand something..smiles.
    All the Best in your life!
    Dy,

    ReplyDelete