![]() |
![]() |
The Dreaded Command LineMay 19, 2003 at 23:55, by Geoff Richards This is an expanded prose version of a short talk on the Unix command line I gave a while back on behalf of GBdirect. A nice PDF version of the slides is available. The Unix and Linux command lineUnix has always been considered a ‘command line’ operating system—and although modern GNU/Linux distributions come with slick graphical interfaces, there's still a place for the ‘old fashioned’ way of doing things. In the early days of Unix (the early 1970s) there was no choice but to use the command line. The minicomputers that Unix ran on were big and slow, and only talked to their users through clunky dumb terminals, which of course didn't support graphics. But programmers and system administrators still use the command line, often only using a graphical user interface (GUI) for web browsing and image editing. This article will look at some of the reasons why this is so, and get you started on using the command line. Pros and ConsThe command line seems slow and inefficient to people who are starting out with it, but with a bit of practice it can be much quicker to use than GUI interfaces, at least for many common tasks. The keyboard can be an efficient way of communicating with a computer, whereas the mouse is much more limited in the amount of information it can convey—a few clicks per second. Even simple tasks like running a GUI program can be quicker—typing ‘mozilla’ to run a web browser is usually quicker than finding it deep in a menu. Typed commands can also express a lot of complex information fairly concisely. The real power of the command line interface is in tying programs together, or running a whole set of related commands in one go. And nor is it as tedious to type commands as once it used to be. Modern Unix command line interfaces have features designed to help you type commands and correct mistakes. This is not to say that the GUI doesn't have a place. Some things, like editing pictures, are certainly better suited to a graphical interface, and non-technical users who just want to do some word-processing or check their email will be better off with them. It takes time to learn the command line well enough to get the benefit of it, so it's not for everyone. How it all fits togetherYour immediate interface to the command line is through a terminal. This is what reads the things you type and displays the text output of the programs you run. When you've typed a command and pressed The whole setup looks something like this:
The terminalThe terminal is what connects the shell to the user. In the olde days of Unix, the terminal would have been a teletype, but nowadays they are usually just a piece of software—a terminal emulator—which behaves like one. Modern terminal software often supports displaying different colours, scrolling back through things that were previously displayed, and displaying a wide variety of characters (example showing Unicode xterm).
There are many terminal emulator programs available. One of the best and most popular is Xterm, which usually comes with X (the Unix graphics subsystem). Xterm is still a good choice, but there are also many alternatives, like the Gnome Terminal (screenshot) and KDE's Konsole (screenshot). These both have more intuitive interfaces for setting preferences. Typically there will be an icon or menu option for launching one of the terminals somewhere on your default desktop. Special terminal features like colour are enabled through special
codes called escape sequences. If a program wants to print
something in green, it just sends the appropriate codes to the
terminal. This is worth knowing, because sometimes the terminal can
be left ‘messed up’, for example if a program crashes. The
Your terminal might be connected directly to your shell, or it might be connected over a network with something like ssh. The command line is a good interface for administering remote machines. There are ways of running graphical programs over a network, but a text interface is much faster, particularly if the network connection is slow. The shell
The term shell refers to the ‘outer layer’ of your software, the part you interact with. In its most general sense it can refer to any kind of user interface software, but under Unix and Linux it always means a command line interface. By far the most popular shell on Linux is Bash. It aims to
be compatible with the original Unix shell, The shell reads the commands you type from the terminal. When you
press Some examples of shell commandsEnough talking, lets see some examples! All the examples in this
article use the The $ whoami geoffr The $ cp report.txt report-backup.txt To view a PostScript file we can run a program called $ gv unix_command_line.ps The $ echo hello, world hello, world Filename completionOne of the most common things typed into a shell is the names of files. Commands are available to copy, rename, and delete files, and many other commands process data from files, so its important that filenames are easy to type. Bash has a feature called filename completion to make this
easier. If you type the start of a filename and then press
$ less rep<Tab> Of course, if there are several files whose names start with the
letters Other completion goodiesBash's Filename completion is also a convenient way of typing filenames which contain ‘special’ characters. If a filename contains spaces or certain punctuation characters, then they have to be escaped to stop Bash from interpreting them specially. The filename completion does this automatically, so you can get away without learning the (fairly complex) rules about how to escape special characters. If you use HistoryThe shell has another feature which can help you reduce typing. It keeps a record of all the commands you type, and allows you to go back and rerun them. This is called the shell history.
To use the history, hit the Some people prefer to use The history is particularly useful if you make a mistake in a command. You can go back to it and edit the command. Use the normal editing keys to change the old command and then rerun it. If you typed a command a long time ago, it can be hard to go back
and find it. Bash lets you search the history to make this easier.
Type GlobbingIf you want to give lots of filenames to a command, the most convenient way is to have the shell automatically collect them. It can build a list of filenames that match a pattern you give and insert them onto the command line, so that you don't have to type them in by hand. For example, to delete all the files in the current directory, use
the $ rm * (Be careful: The The $ wc *.txt For some reason a pattern like this is known as a glob, and so when the shell expands the pattern into a list of filenames it is said to be globbing. Unix has more than its fair share of amusing technical terms. Glob patterns can contain a few other special characters. If a
RedirectionMany commands allow filenames to be given as options (which is why globbing is useful), and some have options for specifying a file in which to put output. But the shell has a more general mechanism for supplying input and output files, called redirection. The shell can be told to connect a command's input or output to
particular files. This is done with the $ cal 2003 >2003.txt Normally the output of the command would be dumped to the
terminal, but The PipingThe concept of piping is very similar to that of redirection, but instead of connecting one program to a file, piping connects two programs together. Everything that the first program outputs is fed straight into the second program. A simple (but fairly useless) example of piping is to connect the
$ echo Happy Birthday | rev yadhtriB yppaH As for redirection, the shell takes care of setting up the pipe, so the programs don't have to know that anything unusual is happening to their output. When the shell starts the commands running, their relationship looks like this: Shell scriptingShells like Bash are actually complete programming languages. It may be that most of the commands you type into a shell are simple commands for manipulating files, but concepts like piping allow them to be used for much more complicated operations. Shell scripting is a way of using the power of the shell to automate common jobs. A shell script is just a set of shell commands, exactly as they would be typed into a shell. It is basically equivalent to a DOS batch file, but the shells flexibility makes it much more useful. So if you find yourself constructing a complicated pipeline of commands it might be worth copying it into a file so that it can be run again. Shell scripting is a useful way of building very simple programs, but the shell language is designed to make typing commands easy, and doesn't have the features necessary for complex tasks. When a shell script gets to be more than a few dozen lines long, it may be time to learn Perl… So…Unix and Linux can be very powerful from the command line. Modern shells make it possible to get work done efficiently from the command line, without as much typing as you might expect. Unfortunately, these advantages come at a price. Shells have a steep learning curve, and there are some jobs which will never be easy to do in a purely text-based environment. But for manipulating files and processing text, the shell is often the best choice. Related Links |
© Copyright Geoff Richards, 2004 |