Tuxfreaks

Are you too????

  • Subscribe to RSS

  • Ads
  • Follow Me on twitter

    twitstamp.com
  • Tweaking With Bash Prompt

    Posted by kranny on September 24th, 2009;This post is viewed 2,854 times

    Originally the shell used in UNIX was sh which stands for shell.Bash is the GNU replacement of sh, and also an extension of it which stands for Bourne-Again Shell.I use bash a lot, and I like it,so I thought I should share my experience with it .Have you ever thought of customising your shell.

    It’s very useful to keep track of system information when you have several Xterms open on several different machines coz one might forget what machine they were working on and delete the wrong files.This a great way to remember what machine you’re on.The appearance of the Bash prompt is controlled by PS1 variable.There is also a second prompt PS2 which is displayed when the intercative shell needs more input to complete a command.

    Before customising your shell,Store your PS1 in a temporary variable;
    kranny@kranny-desktop:~$ TMP=$PS1 //save it in a temporary variable
    kranny@kranny-desktop:~$ PS1=”$ “ //change the prompt to $
    $ date                        //New Prompt
    Thu Sep 24 14:03:39 IST 2009
    $ PS1=$TMP //Reverting to the old Prompt
    kranny@kranny-desktop:~$

    Following are some of the backslash-escaped special characters from Man Page of Bash:
    \a     an ASCII bell character (07)
    \d     the date  in  “Weekday  Month  Date”  format(e.g., “Thu Sept 24″)
    \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
    \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 $
    \]     end a sequence of non-printing characters

    So if You want your prompt to display the current user followed by date in 24 hour format and Date,PS1 should be set as follows:
    kranny@kranny-desktop:~$ PS1=”\u:\@:\d:\$ “
    kranny:02:12 PM:Thu Sep 24:$

    PS: To make the changes permanent edit your .bashrc rather .bash_profile( .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells)

    • Share/Save/Bookmark
    kranny
    1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
    Loading ... Loading ...

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>