Difference between revisions of "Linux: Customize Shell Prompt"

From RHS Wiki
Jump to navigation Jump to search
Line 42: Line 42:
 
# With flag
 
# With flag
 
PS1=$'\\[\e[31m\\]\u2234\\[\e[0m\\]\n \\[\e[31m\\]\u2588\\[\e[33m\\]\u2588\\[\e[31m\\]\u2588  ${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;31m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \\[\e[0m\\]'
 
PS1=$'\\[\e[31m\\]\u2234\\[\e[0m\\]\n \\[\e[31m\\]\u2588\\[\e[33m\\]\u2588\\[\e[31m\\]\u2588  ${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;31m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \\[\e[0m\\]'
 +
 +
PS1=$'\\[\e[31m\\]\u2234\\[\e[0m\\]\n\\[\e[31m\\]\u2588\u2588\u2588\n\\[\e[33m\\]\u2588\u2588\u2588\n\\[\e[31m\\]\u2588\u2588\u2588  ${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;31m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \\[\e[0m\\]'
 
</source>
 
</source>
  

Revision as of 10:02, 28 September 2017

Show current prompt string1

echo $PS1
  • \u --> Username
  • \h --> Hostname
  • \w --> Current working Directory
  • More info @man page bash

To modify edit ~/.bashrc

# Example with Kali prompt
PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "
export PS1

Test color and style with:

echo -e "\033[04;91;46m something \033[00m"
  • 04 --> underline (01 bold)
  • 91 --> ligth red foreground
  • 46 --> cyan background

http://ezprompt.net
https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Colors

Examples:

PS1="\[\e[31m\][\[\e[m\]\[\e[31m\]\u\[\e[m\]@\[\e[31m\]\h\[\e[m\]\[\e[31m\]]\[\e[m\]:\[\e[36m\][\[\e[m\]\[\e[36m\]\w\[\e[m\]\[\e[36m\]]\[\e[m\]\[\e[31m\]\\$\[\e[m\] "
export PS1="\[\e[33m\]\u\[\e[m\]@\[\e[31m\]\h\[\e[m\]:[\W]\\$ "

# New line before, red username yellow@ green host:[blue path]$
PS1="\n\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;32m\]\h\[\033[00m\]:\[\033[01;34m\][\w]\[\033[00m\]\$ "

# red username yellow@ green host:[blue path]$
PS1="\[\e]0;\u@\h: \w\a\]\n\[\e]0;\u@\h: \w\a\]\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;32m\]\h\[\033[00m\]:\[\033[01;34m\][\w]\[\033[00m\]$ "

# Red username yellow @ purple hostname blue path:
PS1="\[\e]0;\u@\h: \w\a\]\[\e]0;\u@\h: \w\a\]\[\033[01;31m\]\n\u\[\033[01;33m\]@\[\033[01;35m\]\h\[\033[00m\]:\[\033[01;34m\][\w]\[\033[00m\]$"

# New Line, red username, yellow @, red foreground and cyan background hostname, blue path
PS1="\[\e]0;\u@\h: \w\a\]\[\e]0;\u@\h: \w\a\]\[\033[01;31m\]\n\u\[\033[01;33m\]@\[\033[04;91;46m\]\h\[\033[00m\]:\[\033[01;34m\][\w]\[\033[00m\]$ "

# New Line, red username, yellow @, red foreground and cyan background hostname, blue path
PS1="\[\e]0;\u@\h: \w\a\]\[\e]0;\u@\h: \w\a\]\[\033[01;32m\]\n\u\[\033[01;33m\]@\[\033[04;91;46m\]\h\[\033[00m\]:\[\033[01;34m\][\w]\[\033[00m\]$ "

# With flag
PS1=$'\\[\e[31m\\]\u2234\\[\e[0m\\]\n \\[\e[31m\\]\u2588\\[\e[33m\\]\u2588\\[\e[31m\\]\u2588  ${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;31m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \\[\e[0m\\]'

PS1=$'\\[\e[31m\\]\u2234\\[\e[0m\\]\n\\[\e[31m\\]\u2588\u2588\u2588\n\\[\e[33m\\]\u2588\u2588\u2588\n\\[\e[31m\\]\u2588\u2588\u2588  ${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;31m\]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ \\[\e[0m\\]'

To test some color:

echo -e "\033[01;31mhi"

Another example:

function elite
{

local GRAY="\[\033[1;30m\]"
local LIGHT_GRAY="\[\033[0;37m\]"
local CYAN="\[\033[0;36m\]"
local LIGHT_CYAN="\[\033[1;36m\]"
local NO_COLOUR="\[\033[0m\]"

case $TERM in
    xterm*|rxvt*)
        local TITLEBAR='\[\033]0;\u@\h:\w\007\]'
        ;;
    *)
        local TITLEBAR=""
        ;;
esac

local temp=$(tty)
local GRAD1=${temp:5}
PS1="$TITLEBAR\
$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\u$GRAY@$CYAN\h\
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\#$GRAY/$CYAN$GRAD1\
$LIGHT_CYAN)$CYAN-$LIGHT_CYAN(\
$CYAN\$(date +%H%M)$GRAY/$CYAN\$(date +%d-%b-%y)\
$LIGHT_CYAN)$CYAN-$GRAY-\
$LIGHT_GRAY\n\
$GRAY-$CYAN-$LIGHT_CYAN(\
$CYAN\$$GRAY:$CYAN\w\
$LIGHT_CYAN)$CYAN-$GRAY-$LIGHT_GRAY " 
PS2="$LIGHT_CYAN-$CYAN-$GRAY-$NO_COLOUR "
}

https://www.maketecheasier.com/8-useful-and-interesting-bash-prompts/
http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html
https://www.linux.com/learn/how-make-fancy-and-useful-bash-prompt-linux
http://misc.flogisoft.com/bash/tip_colors_and_formatting