The Linux Command Shell For Beginners: What is the Shell?

By: Akkana Peck
Monday, December 22, 2008 12:40:15 PM EST
URL: http://www.linuxplanet.com/linuxplanet/tutorials/6624/1/

So What's a "Shell", Anyway?

In the last article I talked about simple command pipelines, one of the features that makes the Linux command line so powerful and so worth learning.

So if you want to get comfortable using the command line, here are some tips that will make it a lot easier.

When you read anything about the command line, you'll hear people use the term "shell", and sometimes terms like "terminal program" or "xterm" or "console". Do they all mean the same thing? Almost! The only difference is whether you are running a graphical environment or not.

If you tried the sample commands in the last article, you were probably running Gnome, KDE, or some other graphical desktop environment, and brought up some sort of window to type them in, maybe through some menu like Applications->System Tools->Terminal. Doesn't matter if you used gnome-terminal, konsole, xfce-terminal or xterm; they're just different types of "terminal windows" or "terminal programs". These are all X terminals, because they run in the X Window System, which is the foundation of all graphical desktops and window managers.

The Linux console is what you see when you do not run a graphical desktop. If you are already in a graphical environment you can see it by pressing CTRL+ALT+F2. (Don't do this just yet!) This takes you to a bare naked login prompt with no GUI. ALT+F1 through ALT+F6 are all consoles. To get back to your graphical desktop, press ALT+F7. (OK, now you can try it.) Most Linux distributions have a "console login" option available in their graphical login managers. Unlike switching back and forth with CTRL+ALT+Fn/ALT+F7, X does not start at all when you use this. But you can start X yourself after logging in to the console with the startx command, so you don't need to log out or reboot.

Any X terminal is nice because you can have multiple tabs or terminals open, and it's easier to configure pretty fonts and colors.

So what's a "shell", anyway?

Okay, so you have a terminal or console running. But in order to use the command line, you need a shell. It is also called a command shell. The shell is the program that reads the command you type, tries to make sense of it, then figures out what to do with it -- which programs to run and where to find them.

Linux has lots of different shells available. Their names almost always end in sh (for shell). The most common shell, and the one you're probably running, is called bash (for "Bourne Again SHell", because it was designed as an extended and Free version of a classic shell written by Stephen Bourne of AT&T's Bell Laboratories). But if you're using a different shell, that's fine; all the basic commands and mechanisms work the same in all shells. You can easily find out what your active shell is with this command:

$ echo $SHELL
/bin/bash

If you need a refresher course in the basics of typing commands, please refer to our last article.

Filename Completion Shortcuts, Copy and Paste

All the modern Linux shells have some features that make life a lot easier for you as you're typing in commands. The first is filename completion.

Somebody sent me a PDF form I need to print out. I want to print it out two-sided so I don't waste paper; and the easiest way I know of to do that is the command line. The file is called Company Name Contract Peck Dec 2008.pdf

I can print out just the odd-numbered pages with something like:

$ lp -o page-set=odd Company Name Contract Peck Dec 2008.ps
except that won't quite work, because shells don't deal well with spaces in filenames. I'd need to add quotes, like this:
$ lp -o page-set=odd "Company Name Contract Peck Dec 2008.pdf"
or add backslashes before the spaces:
$ lp -o page-set=odd Company\ Name\ Contract\ Peck\ Dec\ 2008.pdf

Either way, I don't want to have to type out a monstrosity like that! Fortunately, I don't have to. I can use filename completion.

If I'm already in the directory where the file is, I can type:

$ lp -o page-set=odd C
and then hit the TAB key. One of three things will happen:
  • Magically, "ompany\ Name\ Contract\ Peck\ Dec\ 2008.pdf" will appear after the L I typed, and I'm done!
  • Some part of the name, e.g. "Company\ Name" but not the rest of it, will appear, and the system will beep at me.
  • Nothing will appear, but I'll hear a beep.

In the latter two cases, the beep was the shell's way of telling me that it tried to complete the file name, but there was more than one possible match. For instance, I might have had several files starting with L in the current directory, or several files starting with "Company Name". In that case, all I need to do is type a few more letters and hit tab again, and keep doing that until I get the whole filename.

If you try autocomplete and get beeped at, typing another TAB or two -- if one doesn't do it, try a second -- will list all the possible options.

Autocomplete works for finding names of commands, too. For instance, you may know about the command ls (that's an ell, not a one) to list files in a directory. But try typing ls<TAB><TAB> -- and you'll see all the programs your system have that begin with those two letters. Quite a few!

Copy and Paste in Terminal Programs

Just one more tidbit. If you're used to using Ctrl-C and Ctrl-V to copy and paste, you may find yourself frustrated in a terminal program. In a shell, Ctrl-C aborts whatever command is running; Ctrl-V ensures the next character you type will be taken literally, a way of letting you enter otherwise unprintable characters.

Some terminal programs have an Edit menu with Copy and Paste, but who wants to go to the menus all the time? So don't forget that you can use the other Linux Copy-and-Paste method: just highlight what you want with the mouse, then middle-click in your terminal window. (If you don't have a middle mouse button, try clicking the right and left buttons at the same time.) It's much faster and easier than Ctrl-C/Ctrl-V anyway!

Akkana Peck is a freelance programmer and longtime shell addict. She's also the author of Beginning GIMP: From Novice to Professional, now out in its second edition.

Copyright Jupitermedia Corp. All Rights Reserved.