Facebook Twitter Instagram
    WiredRevolution.com
    • Home
    • About
    • Contact Us
    • Essential Linux Commands
    • Sitemap
    Facebook Twitter Instagram
    WiredRevolution.com
    bash

    Customize the BASH PS1 command prompt

    RyanBy RyanOctober 19, 2008Updated:December 7, 20081 Comment4 Mins Read
    Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email

    The PS1 environment controls the appearance of the BASH command line prompt. There are a variety of default prompts but they usually include username, hostname, and working directory. You can easily customize your prompt to display information important to you as well as add color and style formatting.

    Here we can see the default prompt is “username@hostname working_directory $”.

    ryan@workstation ~ $
    

    You can echo the PS1 environment variable and see the character codes which are used to create the output.

    ryan@workstation ~ $ echo $PS1
    
    \u@\h \w \$
    

    Here is a list of all the special character codes you can use to build your PS1 command prompt.

    Special Character Codes

    \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
    \$ – 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

    Lets try a simple change. Make sure you use double quotes so the escape characters get passed on to the variable and not expanded in the shell.

    ryan@workstation ~ $ PS1="My Prompt: $ "
    
    My Prompt: $
    

    As you can see the prompt will immediately change to reflect the changes.

    We can add the date to the prompt.

    ryan@workstation ~ $ PS1="\d \u@\h \w \$ "
    
    Sun Oct 19 ryan@workstation ~ $
    

    Now lets add some color to our original prompt.

    ryan@workstation ~ $ PS1="\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] "
    

    Let go over this in detail.

    A style block will display all text that follows with the style it defines. The block contains 3 elements.

    Lets look at the first block.
    “\[\e[1;32m\]”

    “\[” – begin a sequence of non-printing characters
    “\e[1;32m” – the semicolon separated list of style/color codes (in this case bold;green)
    “\]” – end a sequence of non-printing characters

    The first block will display the following username@hostname text in BOLD green.
    “\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] ”

    The second block will display the following working_directory text in BOLD blue.
    “\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] ”

    The final block resets the colors.
    “\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] ”

    Here is a rundown of all the color and style codes.

    Color and Style Codes

    Style
    0 – default
    1 – bold
    4 – underline
    7 – inverse
    9 – strikeout

    Foreground Colors
    30 – foreground Black
    31 – foreground Red
    32 – foreground Green
    33 – foreground Yellow
    34 – foreground Blue
    35 – foreground Magenta
    36 – foreground Cyan
    37 – foreground White

    Background Colors
    40 – background Black
    41 – background Red
    42 – background Green
    43 – background Yellow
    44 – background Blue
    45 – background Magenta
    46 – background Cyan
    47 – background White

    You can combine multiple codes to create the exact style you want.
    “\[\e[1;4;36;47m\]”
    This style block defines bold and underlined cyan text with a white background.

    To make these changes permanent place your new prompt string like the one below in your ~/.bashrc file in your home directory.

    PS1="\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$ \[\e[0m\]"
    

    This file is sourced every time you start a new interactive shell.

    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
    Previous ArticleDisplay hostname and IP address in C
    Next Article Search for files with the find command

    Related Posts

    Safely and atomically change a symlink target

    ASCII HTML Reference Chart

    YouTube Begins Transcoding All Video to WEBM

    Most Commented
    March 12, 2009

    Fix blue tinted video in Ubuntu

    September 10, 2010

    Setup SSH access between VirtualBox Host and Guest VMs

    March 8, 2011

    Install GNOME Shell in Ubuntu 10.10 Maverick

    April 4, 2009

    Setup the PS3 Bluetooth Controller on Ubuntu

    October 22, 2008

    How to correctly use LD_LIBRARY_PATH

    Recent Comments
    • Execute command on linux virtual machine (or server) from windows commandline on Setup SSH access between VirtualBox Host and Guest VMs
    • Solved: How to SSH to a VirtualBox guest externally through a host? - Daily Developer Blog on Setup SSH access between VirtualBox Host and Guest VMs
    • How to SSH to a VirtualBox guest externally through a host? [closed] – Code D3 on Setup SSH access between VirtualBox Host and Guest VMs
    • How to copy and paste from VirtualBox? [duplicate] on Setup SSH access between VirtualBox Host and Guest VMs
    • Jackie Laguna on Fix OpenGL: ChoosePixelFormat SketchUp error in WINE
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.