Customising your bash prompt and titlebar

It still surprises me how many servers I log on to that don’t have friendly prompts.  It strikes me as being downright dangerous in the right hands, let alone the wrong ones.

Solaris defaults to csh, HP-UX to ksh and Linux to bash.  Despite having “grown up” on Korn, I much prefer the intuitiveness of bash (command recall using arrow keys!) and would highly recommend all UNIX sysadmins install it.  One word of caution though – If you configure your user account to use bash as it’s default shell, then make sure you have some way into the system in the event that a customisation of your bash configuration goes awry.  HP-UX has a convenient backdoor by means of it’s Service Processor, but you may not be so lucky, subsequently shutting yourself out of your system.  Logging on as root may not be an option for you is local root logins or log in as root over ssh is disabled (common practice in these times of heightened security).  Lastly, don’t change the root users default shell in /etc/passwd.  Stick with the unmodified OS defaults set by the vendor.

OK, with the pitfalls and warnings out of the way, lets customise our bash prompt.  When bash is invoked (either by typing bash at the command prompt, or by your /etc/passwd entry for your user account), the /etc/bashrc file is read to set things up.  I’d not recommend modifying the /etc/bashrc file.  If you want to make customisations that apply only to yourself, then create a .bashrc file in your home directory and play in there.  Don’t forget to chmod +x the file afterwards if you want it to work.

Mine reads as follows

#!/bin/sh

PS1=”\u@\h \w $ “

clear

MYNAM=`grep ${USER} /etc/passwd | cut -d: -f5 | awk {‘print $1’}`

HOSTNAM=`hostname | cut -d. -f1`

echo “${MYNAM}, you are connected to ${HOSTNAM}”

exit

It sets an informative PS1 variable so that the prompt displays my username ‘at’ hostname followed by the present working directory with the obligatory $ prompt on the end.  This is all I need I find, but there are other things you can add if you choose.

\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 + patch level (e.g., 2.00.0)
\w : the current working directory, with $HOME abbreviated with a tilde
\W : the basename of the current working directory, with $HOME abbreviated with a tilde
\! : 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

The remainder of my custom .bashrc creates a couple of additional variables MYNAM and HOSTNAM (don’t use names for variables that can be confused with commands e.g. hostname) which extract my first name from the /etc/passwd file and the hostname up to the first dot in the fqdn returned by the hostname command.  These variables are then used to construct a friendly welcome message when you log in.

I’ve kept it simple, but you can be as elaborate as you like.

More reading here on colours etc (can be useful to colour the root user prompt red for example)…

http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html

CUSTOMISING THE TITLEBAR TO DISPLAY THE CURRENTLY EXECUTING COMMAND

To have the terminal window display the currently running command in the titlebar (useful info during long scrolling outputs), add the following to the .bashrc file in your home directory.

if [ “$SHELL” = ‘/bin/bash’ ]
then
    case $TERM in
         rxvt|*term)
            set -o functrace
            trap ‘echo -ne “\e]0;$BASH_COMMAND\007″‘ DEBUG
            export PS1=”\e]0;$TERM\007$PS1″
         ;;
    esac
fi



				
Did you like this?
Tip cyberfella with Cryptocurrency

Donate Bitcoin to cyberfella

Scan to Donate Bitcoin to cyberfella
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to cyberfella

Scan to Donate Bitcoin Cash to cyberfella
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to cyberfella

Scan to Donate Ethereum to cyberfella
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to cyberfella

Scan to Donate Litecoin to cyberfella
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to cyberfella

Scan to Donate Monero to cyberfella
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to cyberfella

Scan to Donate ZCash to cyberfella
Scan the QR code or copy the address below into your wallet to send some ZCash:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.