The Bash Prompt Posted by: John in Linux on
The Bash Prompt So I have been using Linux as my primary operating system for over a year now. Although I am an avid fan and now despise Windows, Linux does have its annoyances. One of which I have lived with was the bash prompt. The problem I had was, when traversing deep into directories, the bash prompt took up most of the terminal window. For example:
john@lubun2-insp1520:/home/john/www/phpmyadmin/libraries/engines$
This would cause commands to wrap onto the next line and often confuse me. I knew this was possible to change, and below I will explain how you can customize your bash prompt. The string for the prompt is held in an environment variable named PS1 (which is an acronym for Prompt String 1). So see your current prompt, you echo out the variable:
john@lubun2-insp1520:/$ echo $PS1  u@h:w$ 
This probably does not look like what you expected. What you see, are escape sequences. Overriding the environment variable is pretty simple – just redeclare it. This is done using the assignment operator in the shell:
PS1=">"
This has caused my prompt to change to a single right bracket. Using ASCII characters does not provide much customization over your prompt. Below is a list of some common. To see more, see the bash manual (man bash).
  • 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 + patchelvel (e.g., 2.00.0)
  • w the current working directory
  • W the basename of the current working directory
  • ! the history number of this command
  • # the command number of this command
  • To use them, simply escape the character when defining PS1. I like to keep mine simple
     PS1="u:W$ "
    Which will make my prompt look like this:
    john:/$ 
    Now you are almost done. The only problem is, when you restart, these changes will not hold. To do this, you have to export the variable which is done by the following command:
    john:www$ export PS1="u:W$ "
    Thats it!
    Trackback(0)
    feed2 Comments
    Jordan
    June 08, 2008
    Votes: +0

    I like my prompt to to be "user@servername $". And by I mean /home/ftp would display ftp/ IE: "jordan@localhost ftp/ 4". Nice work, thanks!

    report abuse
    vote down
    vote up
    John
    June 08, 2008
    Votes: +0

    I suppose the host name would be useful when ssh-ing to other hosts. I probably should add that back smilies/smiley.gif

    report abuse
    vote down
    vote up

    Write comment
     
     
    quote
    bold
    italicize
    underline
    strike
    url
    image
    quote
    quote
    smile
    wink
    laugh
    grin
    angry
    sad
    shocked
    cool
    tongue
    kiss
    cry
    smaller | bigger
     

    security image
    Write the displayed characters


    busy