March 22, 2019

The Linux Command Shell For Beginners: What is the Shell? - page 2

So What's a "Shell", Anyway?

  • December 22, 2008
  • By Akkana Peck

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 -- 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.

Most Popular LinuxPlanet Stories