Monday, 26 August 2019

Customizing the prompt with PS1

Bash, ksh and sh use enviroment variable $PS1.
Control command for $PS1 are:
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 full hostname j - the number of jobs currently run in background
l - the basename of the shells 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., 4.00)
V - the release of bash, version + patch level (e.g., 4.00.0)
w - Complete path of 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

Standard $PS1
[root@rifle ~]# echo $PS1

[\u@\h \W]\$

Adding date and time to the prompt:
[root@rifle ~]#PS1='[\d \t \u@\h\W\]\$'
[Mon Aug 26 10:30:01 root@rifle~#

Set color to red if use root account:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

To make changes in $PS1 permanent you have to add the PS1 variable to apropriate ~/.bash_profile file. 

Add the following line to the end of /root/.bash_profile file:

export PS1="${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "

To simplify $PS1 construction you can use the following link:

No comments: