Stupid BASH Tricks - Part One
As I’ve been customizing my servers, I’ve come to the time where it is time to customize my shell environment, which leads to Stupid BASH tricks. Some of these are useful, some are not, but they are all Fun.
Colors
One of the easiest ways to customize the Shell environment is to add color, or other formatting to the text. I found this article useful when dealing with color and formatting. Some of the things I learned about doing this is that escape sequences are used to create formatting and color. The characters which can be used are:
- \e
- \033
- \x18
For my customizations I used the \e escapes. The escapes are formatted as \e[NUMBERm. Replace NUMBER with the desired number. The basic codes are:
\e[0m\e[24mWhite\e[103m
####BASH Escape Codes##### | ||
---|---|---|
Code | Description | Escape Code |
Formatting | ||
1 | Bold | \e[1m |
2 | Dim | \e[2m |
4 | Underline | \e[4m |
5 | Blink | \e[5m |
7 | Reverse | \e[7m |
8 | Hidden | \e[8m |
Resets | ||
0 | Reset All Attributes | \e[0m |
21 | Reset Bold | \e[21m |
22 | Reset Dim | \e[22m |
24 | Reset Underline | \e[24m |
25 | Reset Blink | \e[25m |
27 | Reset Reverse | \e[27m |
28 | Reset Hidden | \e[28m |
Foreground Colors | ||
39 | Default Foreground Color | \e[39m |
30 | Black | \e[30m |
31 | Red | \e[31m |
32 | Green | \e[32m |
33 | Yellow | \e[33m |
34 | Blue | \e[34m |
35 | Magenta | \e[35m |
36 | Cyan | \e[36m |
37 | Light Grey | \e[37m |
90 | Dark Grey | \e[90m |
91 | Light Red | \e[91m |
92 | Light Green | \e[92m |
93 | Light Yellow | \e[93m |
94 | Light Blue | \e[94m |
95 | Light Magenta | \e[95m |
96 | Light Cyan | \e[96m |
97 | White | \e[97m |
Background Colors | ||
49 | Default Background Color | \e[49m |
40 | Black | \e[40m |
41 | Red | \e[41m |
42 | Green | \e[42m |
43 | Yellow | \e[43m |
44 | Blue | \e[44m |
45 | Magenta | \e[45m |
46 | Cyan | \e[46m |
47 | Light Grey | \e[47m |
100 | Dark Grey | \e[100m |
101 | Light Red | \e[101m |
102 | Light Green | \e[102m |
103 | Light Yellow | \e[103m |
104 | Light Blue | \e[104m |
105 | Light Magenta | \e[105m |
106 | Light Cyan | \e[106m |
107 | White | \e[107m |
Some terminals can handle more codes than the ones I have listed. These codes work in the majority of terminals which support colors.
BASH Prompt With the escape codes in place, we can create our own unique bash prompt. This is done by simply changing the environmental variable PS1.
With what you want the prompt to be in the quotes.
Would make your bash prompt look like a DOS prompt.
C:\ >
You can create a prompt to indicate if the last command exicuted successfully or not.
In this example a successful completion will generate this prompt: ^_^[jordan@excalibursheath.com:~] $
A uncessful compleation will generate this prompt: O_O[jordan@excalibursheath.com:~] $
This prompt uses the if statement which evaluates the exit status of the most recent foreground (The command you ran) pipeline exit status. Successful completion will equate to 0, an error will equate to 1.
You can use this same logic to indicate success or not in many different ways, like changing the color of the prompt.
There are variables you can add into the prompt, to get various information at the commandline.
- \n - New Line
- \r - Carraige Return
- \l - Basename of the shell’s terminal device name
- \s - Name of the Shell (basename of the $0 variable, or everything after the final //)
- \j - Number of jobs currently managed by the shell
- \a - ASCII bell character
- \e - ASCII escape character
- \d - Date
- \D{format} - The format is passed to strftime
- \t - Time in 24 hour HH:MM:SS Format
- \@ - Time in 12 hour HH:MM:SS Format
- \A - Time in 24 hour HH:MM Format
- \w - Full Path
- \W - Basename of the current working directory
- \u - Username
- \h - Hostname
- \H - Full hostname
- \v - Version of BASH
- \V - The release of BASH (version + patches)
- ! - History number of the last command
- # - Command number of the command
- $ - Effective UID
- \ - a Backslash
- $? - Status of the last command
- $kernel_version - The output of the uname -r command ($kernel_version variable)
Scripts can also be incorporated into the BASH prompt, which makes the prompt really customizable.
Colorizing the Prompt Remeber those sequences above? You can add them into a BASH prompt to add color to your terminal.
This is an example of my current prompt.
You add the escape codes into the PS1=”before the text you want to change, then when you want to reset the formatting use the \e[0m code.
Between Formatting, variables, and scripts you can make a customized BASH prompt which you like.
Fortunes and Fun Fortune is a fun little program, which has been with Linux from the beginning, and Unix even longer. The whole point of fortune, is to display a somewhat random quote, or fortune on the screen. Fortune stores its quotes into “databases”, and you can specify things like how many characters you want the fortune, and which fortune files (usually topics) you want to draw from.
fortune can be called, just by its name:
However, you can use the -s flag to only get short fortunes; by default short fortunes are 160 characters or less. You can use percentages to be sure that you get certain fortunes more often. The example from the man page is if you have funny and not-funny calling fortune 90% funny not-funny will provide fortunes from the funny file 90% of the time.
I like the following command:
This outputs a selection of fortunes I like, in color!
Cowsay is a fun program which can take random text and put it in a bubble for a ASCII Character to say.
Cowsay has the following options:
- -f - Cow file, depending on your Linux distribution you will find cows in /usr/share/cows
- -b - Borg Mode
- -d - Dead Mode
- -g - Greedy Mode
- -p - Paranoia Mode
- -t - Wired Mode
- -y - Youth Mode
- -e - Select the characters of the eyes
- -T - Select the Toung Character
- -W - text wrpar width, default 40 characters With all of this in mind The fortune/cowsay command I like is:
Conclusion Now you can customize your prompt, and add a fun fortune/cowsay command to your .bashrc file for some fun when you log into your system. Join me next time as I show you how to further customize your environement, and share with you the Stupidest BASH trick of them all.