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,
- Bash shell family.
- 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,
Colour code and escape sequence list:
\e[SS;RRm]
SS = foreground colour
RR=background colour, but it is really text styling
Colour Table
| Intensity | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| Normal | Black | Red | Green | Yellow | Blue | Magenta | Cyan | White | reset |
| Bright | Black | Red | Green | Yellow | Blue | Magenta | Cyan | White |
SGR(Select Graphic Rendition) parameters
| Code | Effect | Note |
|---|---|---|
| 0 | Reset / Normal | all attributes off |
| 1 | Intensity: Bold | |
| 2 | Intensity: Faint | not widely supported |
| 3 | Italic: on | not widely supported. Sometimes treated as inverse. |
| 4 | Underline: Single | |
| 5 | Blink: Slow | less than 150 per minute |
| 6 | Blink: Rapid | MS-DOS ANSI.SYS; 150 per minute or more |
| 7 | Image: Negative | inverse or reverse; swap foreground and background |
| 8 | Conceal | not widely supported |
| 21 | Underline: Double | not widely supported |
| 22 | Intensity: Normal | not bold and not faint |
| 24 | Underline: None | |
| 25 | Blink: off | |
| 27 | Image: Positive | |
| 28 | Reveal | conceal off |
| 30–39 | Set foreground color, normal intensity | 3x, where x is from the color table above |
| 40–49 | Set background color, normal intensity | 4x, where x is from the color table above |
| 90–99 | Set foreground color, high intensity | aixterm |
| 100–109 | set background color, high intensity | aixterm |
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
Post a Comment