Excalibur's Sheath

Stupid BASH Tricks - Part One

May 5, 2016 • linux,bash

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#####
CodeDescriptionEscape Code
Formatting
1Bold\e[1m
2Dim\e[2m
4Underline\e[4m
5Blink\e[5m
7Reverse\e[7m
8Hidden\e[8m
Resets
0Reset All Attributes\e[0m
21Reset Bold\e[21m
22Reset Dim\e[22m
24Reset Underline\e[24m
25Reset Blink\e[25m
27Reset Reverse\e[27m
28Reset Hidden\e[28m
Foreground Colors
39Default Foreground Color\e[39m
30Black\e[30m
31Red\e[31m
32Green\e[32m
33Yellow\e[33m
34Blue\e[34m
35Magenta\e[35m
36Cyan\e[36m
37Light Grey\e[37m
90Dark Grey\e[90m
91Light Red\e[91m
92Light Green\e[92m
93Light Yellow\e[93m
94Light Blue\e[94m
95Light Magenta\e[95m
96Light Cyan\e[96m
97White\e[97m
Background Colors
49Default Background Color\e[49m
40Black\e[40m
41Red\e[41m
42Green\e[42m
43Yellow\e[43m
44Blue\e[44m
45Magenta\e[45m
46Cyan\e[46m
47Light Grey\e[47m
100Dark Grey\e[100m
101Light Red\e[101m
102Light Green\e[102m
103Light Yellow\e[103m
104Light Blue\e[104m
105Light Magenta\e[105m
106Light Cyan\e[106m
107White\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.

export PS1=""

With what you want the prompt to be in the quotes.

export PS1"C:\ > "

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.

PS1="`if [ $? = 0 ]; then echo ^_^; else echo O_O; fi`[\u@\h:\w]$"

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.

export PS1="\e[32m\A\n\e[93m\w\n\e[91m\u\e[0m@\e[36m\h\e[0m $ "

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:

fortune

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:

echo -e "\e[33m$(fortune 75% linux 5% computers 10% perl 10% startrek)\e[0m"

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:
echo -e "\e[33m$(fortune 75% linux 5% computers 10% perl 10% startrek)\e[0m" | cowsay -f dragon

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.