Customizing Terminal command prompt

The power of UNIX operating systems, is it's terminal or in other words, command line interfaces.

These interfaces, operates as interpreters, that interprets the human readable text inputs in to a language the, underlying kernel (OS) can understand and perform the task, what it has been asked to do by the user through the kernel.

UNIX and Linux has always been criticised as not being user friendly, because they lacks, eye candy GUI (Graphical User Interface). But, the real power of UNIX is its command line interfaces (CLI).

Shell is what, the CLI. There are many shells available and each has its own pro's and con's and own community of users. What is going to be discussed here is, bash shell.

Bash - the default shell in UNIX-7. Since, then it is the default shell in many Unix and Linux variants.

Shells can broadly be classified into, two big families,

  1. Bash shell family.
  2. C shell family.
The default shell in Macintosh 10.5 is bash, though Macintosh ships with ksh, tcsh, zsh...

A shell can be said to be either interactive or non-interactive. Interactive shell is the one, which needs input from the user. Once, it completes the task, it waits for the next user input.

Open the terminal in your system and type the following in your terminal command prompt,

> whoami
> date

Once "whoami", it will spit out your username in that particular system. It will prompt you the command prompt, as shown in the following,


Until the next command, shell will be idle. Upon typing the next command, will show the date and time.

This is interactive shell, where it needs input form the user. This is what most unix users interacts with most of the times.

Non-interactive shell, doesn't wait for the user input, every time it carries out an action. All the startup scripts in Linux OS and UNIX systems are run in non-interactive shells. Although, interaction is possible.

create a file with .sh extension and type in the two commands and change it a executable in any UNIX machine. Run the file, it will spit out both the outputs, that we got after typing each individual commands.

Prompt is the one which, announces the completion of the previous task or command and prompts the user for the command.

Before a shell starts, it reads in its configuration files to assimilate its defined behaviour. The order of precedence of the files in Macintosh is as follows,
/etc/.bashrc, /etc/.profile /private/.bashrc, /private/profile, ~/.bashrc and ~/.bash_profile

So whatever, is defined in the ~/.bash_profile will override its previous settings.

Prompt in the shell can be customized to our convenience. Here is an example,


russell@[83%] - is my prompt. It tells, the username using which I logged in to this computer. The value followed by username is the current battery level, in my laptop. It is another script!!

username appears in blue in colour where as "@" symbol appears in white and the following battery level in green. Infact battery level colour will change as it lowers down certain value.

My .bashrc file for this part is as follows,

# - - # - - # - - #
#Sourcing global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

#Command prompt colour and appearence

function prompt
{
local WHITE="\[\033[1;37m\]"
local GREEN="\[\033[0;32m\]"
local CYAN="\[\033[0;36m\]"
local GRAY="\[\033[0;37m\]"
local BLUE="\[\033[1;34m\]"
local BLACK="\[\033[0;30m\]"
local BLACKR="\[\033[1;30m\]"
local MAGENTA="\[\033[0;35m\]"
local RED="\[\033[0;31m\]"
local BLACKBG="\[\033[1;40m\]"
export PS1="${BLUE}\u${GRAY}@\$(bat) ${MAGENTA}\W:${WHITE}"
}
prompt
# - - # - - # - - #

export PS1 - the one responsible for my prompt behaviour and colour, which is a UNIX shell environment variable.
I have defined colours under the function prompt, and used them for PS1 variable.

One can use their own colours and wishes. In this example, the user and the working directory are prompted. But, one can chose from a wide variety of options available in any UNIX system.

List of Prompt variables,




\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
Colour code and escape sequence list:

\e[SS;RRm]
SS = foreground colour
RR=background colour, but it is really text styling

Colour Table

Intensity012345679
NormalBlackRedGreenYellowBlueMagentaCyanWhitereset
BrightBlackRedGreenYellowBlueMagentaCyanWhite

SGR(Select Graphic Rendition) parameters

CodeEffectNote
0Reset / Normalall attributes off
1Intensity: Bold
2Intensity: Faintnot widely supported
3Italic: onnot widely supported. Sometimes treated as inverse.
4Underline: Single
5Blink: Slowless than 150 per minute
6Blink: RapidMS-DOS ANSI.SYS; 150 per minute or more
7Image: Negativeinverse or reverse; swap foreground and background
8Concealnot widely supported
21Underline: Doublenot widely supported
22Intensity: Normalnot bold and not faint
24Underline: None
25Blink: off
27Image: Positive
28Revealconceal off
30–39Set foreground color, normal intensity3x, where x is from the color table above
40–49Set background color, normal intensity4x, where x is from the color table above
90–99Set foreground color, high intensityaixterm
100–109set background color, high intensityaixterm


After these settings in .bashrc, .bash_profile should look something like this,
# - - # - - # - - # - - #
#Get the aliases and functions from the bashrc file

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

#Listing directories and files in clours based on their properties

export CLICOLOR=1
export LSCOLORS=exfxcxdxbxexedabagacad
# - - # - - # - - # - - #

The colours exported here are for differentiating filetypes based on their characteristcs.

With this, anyone can customize their own prompt and terminal behvaiour in Macintosh-10.5

Leave a comment (of your mailid), if you want my .bashrc.

Comments