/  522
 
The Linux
®
Command Line
William E. Shotts, Jr.
A LinuxCommand.org Book
 
Copyright ©2008-2009, William E. Shotts, Jr.This work is licensed under the Creative Commons Attribution-Noncommercial-NoDerivative Works 3.0 United States License. To view a copy of this license, visithttp://creativecommons.org/licenses/by-nc-nd/3.0/us/or send a letter to CreativeCommons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.Linux
®
is the registered trademark of Linus Torvalds. All other trademarks belong totheir respective owners.This book is part of the LinuxCommand.org project, a site for Linux education andadvocacy devoted to helping users of legacy operating systems migrate into the future.You may contact the LinuxCommand.org project athttp://linuxcommand.org.Printed copies of this book, in large, easy-to-read-format, are available forpurchase from lulu.com. Orders may be placed at the following:http://www.lulu.com/content/paperback-book/the-linux-command-line/7594184
Release History
 
Version Date Description
09.12 December 14, 2009 First Edition.09.11 November 19, 2009 Fourth draft with almost all reviewer feedbackincorporated and edited through chapter 37.09.10 October 3, 2009 Third draft with revised table formatting,partial application of reviewers feedback andedited through chapter 18.09.08 August 12, 2009 Second draft incorporating the first editingpass.09.07 July 18, 2009 Completed first draft.
 
Table of Contents
Part 1 – Introduction.........................................................................1
1 – Introduction................................................................................................2
Why Use The Command Line?.......................................................................................2What This Book Is About.................................................................................................3Who Should Read This Book..........................................................................................3What's In This Book.........................................................................................................4How To Read This Book..................................................................................................5Prerequisites...............................................................................................................5Why I Don't Call It “GNU/Linux”.............................................................................5Acknowledgments...........................................................................................................6Your Feedback Is Needed!..............................................................................................7Further Reading...............................................................................................................7Colophon.........................................................................................................................7
Part 2 – Learning The Shell..............................................................9
2 – What Is The Shell?...................................................................................10
Terminal Emulators........................................................................................................10Your First Keystrokes....................................................................................................10Command History.....................................................................................................11Cursor Movement.....................................................................................................11A Few Words About Mice And Focus...................................................................11Try Some Simple Commands........................................................................................12Ending A Terminal Session............................................................................................13The Console Behind The Curtain........................................................................13Further Reading.............................................................................................................13
3 – Navigation.................................................................................................14
Understanding The File System Tree............................................................................14The Current Working Directory......................................................................................14Listing The Contents Of A Directory..............................................................................15Changing The Current Working Directory.....................................................................16Absolute Pathnames................................................................................................16Relative Pathnames.................................................................................................16Some Helpful Shortcuts............................................................................................18
i
 
Adding Color
<me@linuxbox ~>$
PS1="\[\033[0;31m\]<\u@\h \W>\$"
<me@linuxbox ~>$
That works, but notice that all the text that we type after the prompt is also red. To fixthis, we will add another escape code to the end of the prompt that tells the terminalemulator to return to the previous color:
<me@linuxbox ~>$
PS1="\[\033[0;31m\]<\u@\h \W>\$\[\033[0m\]"
<me@linuxbox ~>$
That's better!It's also possible to set the text background color using the codes listed below. Thebackground colors do not support the bold attribute.
Table 14-3: Escape Sequences Used To Set Background Color
Sequence Background Color Sequence Background Color
\033[0; 40m
Black
\033[0; 44m
Blue
\033[0; 41m
Red
\033[0; 45m
Purple
\033[0; 42m
Green
\033[0; 46m
Cyan
\033[0; 43m
Brown
\033[0; 47m
Light GreyWe can create a prompt with a red background by applying a simple change to the firstescape code:
<me@linuxbox ~>$
PS1="\[\033[0;41m\]<\u@\h \W>\$\[\033[0m\]"
<me@linuxbox ~>$
Try out the color codes and see what you can create!
Note:
Besides the normal (0) and bold (1) character attributes, text may also begiven underscore (4), blinking (5), and inverse (7) attributes as well. In theinterests of good taste, many terminal emulators refuse to honor the blinkingattribute, however.165
 
14 – Customizing The Prompt
Moving The Cursor
Escape codes can be used to position the cursor. This is commonly used to provide aclock or some other kind of information at a different location on the screen such as anupper corner each time the prompt is drawn. Here is a list of the escape codes thatposition the cursor:
Table 14-4: Cursor Movement Escape Sequences
Escape Code Action
\033[
l
;
c
H
Move the cursor to line
l
and column
c
.
\033[
n
A
Move the cursor up
n
lines.
\033[
n
B
Move the cursor down
n
lines.
\033[
n
C
Move the cursor forward
n
characters.
\033[
n
D
Move the cursor backward
n
characters.
\033[2J
Clear the screen and move the cursor to the upper left corner (line0, column 0).
\033[K
Clear from the cursor position to the end of the current line.
\033[s
Store the current cursor position.
\033[u
Recall the stored cursor position.Using the codes above, we'll construct a prompt that draws a red bar at the top of thescreen containing a clock (rendered in yellow text) each time the prompt is displayed.The code for the prompt is this formidable looking string:
PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]<\u@\h \W>\$"
Let's take a look at each part of the string to see what it does:
Sequence Action
\[
Begins a non-printing character sequence. The real purpose ofthis is to allow
bash
to correctly calculate the size of thevisible prompt. Without this, command line editing featureswill improperly position the cursor.
\033[s
Store the cursor position. This is needed to return to the prompt166
 
Moving The Cursorlocation after the bar and clock have been drawn at the top ofthe screen.
Be aware that some terminal emulators do not honor this code.
\033[0; 0H
Move the cursor to the upper left corner, which is line zero,column zero.
\033[0; 41m
Set the background color to red.
\033[K
Clear from the current cursor location (the top left corner) tothe end of the line. Since the background color is now red, theline is cleared to that color creating our bar. Note that clearingto the end of the line does not change the cursor position, whichremains at the upper left corner.
\033[1; 33m
Set the text color to yellow.
\t
Display the current time. While this is a “printing” element, westill include it in the non-printing portion of the prompt, sincewe don't want
bash
to include the clock when calculating thetrue size of the displayed prompt.
\033[0m
Turn off color. This affects both the text and background.
\033[u
Restore the cursor position saved earlier.
\]
End non-printing characters sequence.
<\u@\h \W>\$
Prompt string.
Saving The Prompt
Obviously, we don't want to be typing that monster all the time, so we'll want to store ourprompt someplace. We can make the prompt permanent by adding it to our
. bashrc
file. To do so, add these two lines to the file:
PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]<\u@\h \W>\$"export PS1
Summing Up
Believe it or not, there is much more that can be done with prompts involving shellfunctions and scripts that we haven't covered here, but this is a good start. Not everyone167

Sections

show all« prev | next »

Share & Embed

More from this user

Add a Comment

Characters: ...