Bash is the primary shell of the Linux machine, included here are some tips/tricks with the shell. Use the manual page and learn about PS1, PS2 and PROMPT_COMMAND. The tricks in here can be use in ~/.bash_profile.
Output in Colour
Cool terminal output involves color, here is how to set and reset output colors using terminal control characters. To print in color one must first send the control sequences for the colour to the terminal, then output the message and reset the terminal, to be nice. One can also create nifty looking prompts with the teniques described here.
Consoloe Colour Table
The below key describes all the different sequences that can be sent to change the display format. The disclaimer should be made that "this is how the test computer performed".
Style Foreground Background 1st Digit 2nd Digit 3rd Digit 0 - Reset 30 - Black 40 - Black 1 - FG Bright 31 - Red 41 - Red 2 - Unknown 32 - Green 42 - Green 3 - Unknown 33 - Yellow 43 - Yellow 4 - Underline 34 - Blue 44 - Blue 5 - BG Bright 35 - Magenta 45 - Magenta 6 - Unknown 36 - Cyan 46 - Cyan 7 - Reverse 37 - White 47 - White
To instruct echo
to interpret these codes you must tell it -en
.
The -e
switch tells echo to interpret your escape sequences and -n
tells echo not to make a newline at the end.
The activation sequence is \033
this starts the output modification codes.
Next is a [
followed by a digits from the above list to represent the style, foreground and background.
The sequence is terminated by the letter m.
The sequence to get plain red on black would look like this: echo -en "\033[0;31;40m"
or once could say echo -en "\033[0;31m"
to only affect the foreground.
Portions of the sequence can be left out but the digits are still interpreted in the same order.
One can switch only the background by saying echo -en "\033[7;43m"
, this would change the background to yellow without affecting the current foreground settings.
Once output is complete the terminal should be reset with echo -e "\033[0m"
.
Echo in Colour Test Script
The code below is a sample of echo in colour, it will run through all the sequences and ouput some nice looking tables. This can be use to test what different colours will look like as well as show the hidious combinations.
#/bin/sh # Show all the colours of the rainbow, should be run under bash for STYLE in 0 1 2 3 4 5 6 7; do for FG in 30 31 32 33 34 35 36 37; do for BG in 40 41 42 43 44 45 46 47; do CTRL="\033[${STYLE};${FG};${BG}m" echo -en "${CTRL}" echo -n "${STYLE};${FG};${BG}" echo -en "\033[0m" done echo done echo done # Reset echo -e "\033[0m"
Setting Terminal Title
When using a program like PuTTY to access the Linux box sometimes it's nice to have an informative termnal.
Set my title to: hydrogen.edoceo.com # echo -en "\e]0;hydrogen.edoceo.com\a" Set my title to: Title # echo -en "\e]0;Title\a"