Bash Tutorial: What's My Line?
java.emitter at gmail.com
Sun Feb 2 09:33:54 MST 2014
*The Command Line*
Bash is said to have a command line interface (CLI) because what you
literally do is type a command to your computer (or a remote one) on a
single line. Well, sometimes it's more than one line, but let's pretend
it's only one for now.
Commands that you type at the command line are said to be interpreted
because they are translated into instructions the computer can understand.
If you were using a human interpreter (translator) to speak to someone in a
foreign language like Australian, you'd likely say a single sentence (a
line) to them and have it translated/interpreted. The interpreter would
not translate every syllable as you spoke. Nor would he wait for you to
tell your life story before doing any translation. The former situation is
how input to a video game works; the latter, a compiler. Bash is neither.
Just like with a human interpreter, the intent of your message may not
actually be communicated. (Indeed, some people need to be told the same
thing over and over again for the space of many months before it finally
registers.) You may think you told the computer to back up all your files
when in fact you have told it to erase them all. The problem is you need
to be able to translate your natural language desire into a Bash command
that bash can then translate into something the computer can understand.
In fact, Bash is both an interactive interpreted language and a program
(the interpreter). It is also an interpreted scripting language. When I
am talking specifically about the interpreter program, I will say 'bash'.
When I am talking about the interpreted interactive/scripting language that
the interpreter program interprets (executes), I will say 'Bash'. This
paragraph is probably a good example of intended messages not being
*Let's Bash On the Keyboard*
Just like a baby makes sounds as it attempts to form words, you need to
learn how to edit a line of Bash even if you can't actually compose a
useful command yet. The line editing facilities for Bash are provided by
the readline library (http://en.wikipedia.org/wiki/GNU_Readline). The
terminal passes the character stream it receives essentially raw to the
readline library that then passes the final composed line on to bash. It
is configured for your user account by the ~/.inputrc file (and system-wide
by /etc/inputrc). For now, leave them alone.
I use a Vimish style to describe key combos. So, <C-V> means "press the
control key and v key at the same time". Even though the V is uppercase,
you do not uppercase it using the shift key. If I want you to press
<Control + Shift + S>, I'll say <C-S-V>. <Alt + A> is <A-A>. <Window + M>
is <W-M>. When I say "jump", you say "how high?". Actually, you say
Here are many of the editing commands you can use on the bash command line:
<C-A> Go to the beginning of the line.
<C-E> Go to the end of the line.
<A-F> Move forward one word. Unfortunately, in GNOME Terminal on Ubuntu,
this opens the File menu by default. Fortunately, this fixes the problem:
Edit | Keyboard Shortcuts... | uncheck Enable menu access keys.
<A-B> Move backward one word.
<C-F> Move forward one character. You can use the <Left> and <Right> arrow
keys to move back and forth one character at a time. You should not do
this because it forces you to move your hand away from the home row keys.
<C-B> Move backward one character.
<S-PgUp> Scroll back a page in the terminal display buffer.
<S-PgDn> Scroll forward a page in the terminal display buffer.
<C-U> Delete everything left of the cursor (to the beginning of the line).
<C-K> Delete everything (kill) to the right (to the end of the line).
<C-H> Go to hell. Well, at least it sends the character to the left
there! Your backspace key should also delete the character to the left.
<C-W> Delete word to left.
<A-BS> Delete word to left.
<C-P> Recall previous command from history. Press multiple times to go
back deeper into history. So, this is kind of like the back button on your
web browser. While you can also use the <Up> and <Down> arrow keys to
navigate history, I do not recommend doing so because this forces you to
move a hand away from the home row keys. In general, avoid this because it
will slow you down.
<C-N> Move to next command in command history.
<C-L> Clear the screen. Note that the 'clear' command also does this.
Beware on Windows of accidentally hitting <W-L> because this will lock your
screen. This has happened to me more times than I care to admit.
<S-Insert> Usually paste from system clipboard.
<C-Y> Paste whatever you last deleted.
<C-T> Transpose (swap) the characters on either side of the cursor.
<A-T> Transpose (swap) the two words to the left of the cursor.
<C-_> Undo. Interestingly, there does not seem to be a redo command.
<C-R> Search backwards through command history. After you find the command
you want, press <C-E> to start editing at the end of it. If your search
string matches multiple commands, use <C-P> and <C-N> to navigate between
them. If for some reason, command history is not enabled, I apologize. We
will enable and fully utilize it in the future.
<A-.> Paste the last word (argument) from previous commands. Press
multiple times to cycle back through history. This editing command is VERY
<Tab> Autocomplete current word. Certainly one of the most useful editing
keystrokes to know. Works so much better than the stupid Windows/DOS
<Tab><Tab> Show all completions of the current word (if there are multiple).
<C-C> Interrupt the current foreground process (usually terminates it).
<C-Z> Suspend the current foreground process.
<C-D> Emit an end-of-file (EOF) character (ASCII value 4).
http://en.wikipedia.org/wiki/End-of-file. This may or may not cause your
bash session to abruptly end.
<C-V> Literally insert the next character as data instead of interpreting
it as a terminal control code. Notice that this conflicts with the usual
Windows definition as "paste from clipboard". This reminds you that you
are in fact talking to a (virtual) terminal. It only sees a stream of
characters. It does not see key events with modifiers. It does not
actually know if you have Ctrl, Alt, and Shift pressed at the same time.
So, if you needed to enter a literal <C-V> on the command line, you type
<C-V><C-V>. Note that Vim also uses this mechanism.
<C-Tab> Cycles forward through Cygwin windows. If you have a multitab
terminal emulator, this will likely cycle forward through tabs. MTPuTTY is
a good multitab version of PuTTY.
<C-S-Tab> Cycles backwards (to the left) through terminal windows/tabs.
<C-S> No, it's not "save file". It means "shut up". Sort of. Remember
when I told you about flow control in terminals? I thought not. Go back
and read the last tutorial. Now, then. This is the character used to send
an Xoff message for software-based flow control. What this means is the
computer will stop transmitting back to your terminal screen. So, it will
appear as though your terminal has locked up (even though the computer
actually is still executing any commands it receives from you). In the
future, I'll show you how to edit your bash configuration files to disable
this control character so that you don't accidentally freeze your
terminal. Really, a human has no business emitting flow control codes.
Unfortunately, if you are like me, you're hands just automatically emit a
<C-S> every few seconds without being consciously aware of it.
<C-Q> Sends the Xon flow control message. This will unlock your terminal
if you locked it via <C-S>. The chances of you remembering this are
approximately zero, however.
<C-M> Carriage return.
<C-J> Line feed.
<A-number> Should emit the character whose value is whatever number you
type in before releasing the Alt key. However, on Cygwin, it is
interpreted as the number of times to repeat whatever character you enter
next. So, I'm a bit confused on this one. Back in the days of the IBM PC,
we used to use this to type smiley faces and other weird symbols into the
computer. It was like a symbol lottery: we didn't know what would pop up
on the screen! This was long before Forrest Gump enlightened us on how
boxes of chocolate could provide similar surprises.
Most other keys you press should just emit their usual character and not do
anything special (although you do know it is quite remarkable for even a
single character to appear).
Note some useful features of bash these editing commands hint at: command
history, autocompletion, job/process control, configuration files, and
positional arguments. That's just the tip of the ice cube! Each of these
topics will be covered in depth in future tutorials.
[Indeed, I am aware of the vi editing mode for bash. To my dismay, it
never seemed to work correctly even after fiddling with it for hours, so I
use the default emacs key bindings which at least work as advertised.]
*Perfect Practice Makes Perfect*
Some people have 10 years of experience; some people have the same year of
experience 10 times. Type in a sentence like "the quick brown fox jumped
over the lazy dogs" on the command line. Edit it in various ways. Delete
that sucker in one keystroke. Now who's quick, fox? What does the fox say
to that? Try to use all the editing commands mentioned above. Type in
another sentence like "sphinx of black quartz, judge my vow". Try using
<C-R>, <C-P>, <C-N>, and <C-.> to navigate history. Resist the urge to use
the arrow keys. For now, don't worry if your commands don't do anything.
That said, I take absolutely no responsibility if they wreck your machine.
Obviously, don't type random commands into a production machine that
contains the only pictures you have of your family because you never
bothered to back them up anywhere: always mount a scratch monkey (
http://en.wikipedia.org/wiki/Scratch_monkey). ls before you rm. SELECT
before you DELETE. Look before you leap. Bonus points if you recognize
what is special about the two example sentences.
*Pro Tip: Tactics vs. Strategy*
Remember kids, no matter how fast you can type, how efficient your command
line skills are, or how many hours a week you put in at work, none of it
can make up for consistently bad strategic decisions from management. So
always fight the strategic battle incessantly as you hone your tactical
skills. If you can't win the strategic battle, find a more profitable game
to play where your tactical skills will not be wasted.
Now that you can type a line of text into bash, I will next give you the
power to command the computer via Bash. So, get your mouse ready, and
practice saying, "transparent aluminum", Scotty. When I return, we shall
type in some magical invocations that Richard Stallman refers to as
'cantrips'. I ask only a few zorkmids (approximately a billion Dogecoin)
for admission. If you don't have any gold, I suppose you can just stay a
while and listen.
More information about the PLUG