Command line

From MobileRead
Jump to: navigation, search

A Command line or Command Line Interface (CLI) is a way to tell a computer what you want it to do using a keyboard. This is one of the ways to instruct a computer as to what you want. Others include a graphical user interface (GUI) and a menu interface. A computer may have support for all three methods. For example a PC has a toolbar at the bottom of the screen with the word 'start.' You can use the GUI interface to bring the mouse to the word 'start' and then click on it. The menu interface will appear. You can traverse the menu interface using the mouse or the arrow keys. On the menu interface you will see Run.... If you select this option a command line will appear for you to type in a command.

Contents

[edit] Overview

The CLI generally has both an input method, the keyboard, and an output method, a screen display but the output method is not needed if the command input issues a command that has its own display capabilities. For example you might use the Run... choice from the "Start" menu and type in the word 'notepad' and then hit the Enter key. After a few seconds the notepad application will display its normal graphic screen. (The enter key told the computer that you were done typing the command.) See: Command Prompt Vista/XP/Mac for examples of how to get to a command line.

Often, though, you want both an input method and an output display. For this you will usually need a shell. One way to get a shell in Windows is to use the same Run... menu item but this time enter the word 'cmd' and tap the enter key. This time you will get a CMD (command) window with a prompt at the bottom. To end the CMD window session you can enter the command 'exit' at the prompt. While today a CMD window is a command line shell for Windows, it emulates and is modeled after an earlier OS called PC DOS, or MS DOS (commonly called DOS, Disk Operating System). Often the CMD window is called a DOS window for this reason.

Input to a shell is like writing a sentence to the computer and telling it what to do. Since you want it to do something you will usually start with a verb, for example copy, to tell it the basic command you want. The command will be followed, sentence style, with other words that tell it the name of the file you want to start with, source, and the name or place you want to end with, destination. Then hit the enter key to cause the command to be executed. For example:

copy file.txt file.bak

would make a backup copy of file.txt. When the command is finished it will provide a prompt again for the next command. The spaces between the words tell the computer where the boundaries of the arguments are. Arguments are command line parameters, nouns, that are the object of the command. In Unix/Linux the same command would be:

cp file.txt file.bak

as Unix basic utility commands are usually shortened to minimize the amount of typing. In the discussion on this page the word Unix is used in the generic sense to apply equally to variations in Unix systems such as Linux, SunOS, etc.

MacOS X can also have a Unix like command line window. MacOS X is a Mac GUI implementation setting on top of a version of Unix. The shells can be reached and they can be used just like other versions of Unix.

When typing you should know that upper/lower case usually does not matter in Windows but does matter in Unix. If you try a command and the filenames do not work then try 'dir' for windows or 'ls' for Unix to see the list of file names on the screen. It may be that you typed it wrong.

[edit] Syntax

A command line consists of a sentence (a group of words separated with spaces). The first word is the command name. It is optionally followed with a set of arguments. These are generally the objects that are acted upon by the command. In the copy examples shown in the previous section the first argument was the old file name while the second argument is the new file name of the copy. If the new name already exists a prompt will appear asking you if you want to replace the file. Answer Y to complete the command or N to cancel. Different commands have differing requirements for the number of arguments and how they are to be interpreted.

You can also change the behavior of commands with options. An option generally is entered first before the list of arguments but some commands allow them to be entered later. In windows an option is usually a single letter and is preceded with a /. For example entering copy/? in windows will cause the copy command to show a help screen to explain how to use the command. In Unix a - or a -- is used to specify an option. This must be separated with a space from the command name. the equivalent help command would be cp --help. If a command needs arguments and you type its name without any you will get an error message that often includes the syntax.

[edit] Help

Since every command can have a different set of arguments and options there is a need for online help to guide you to the correct syntax. As already mentioned the command itself may include minimal help but there is also an online manual that can provide additional help. In windows you can type 'help' and then the name of the command you want help about. This will not work on all commands but can still be useful for some. For a list of covered commands type 'help' by itself.

In Unix the convention is to type 'man' (short for manual) followed by the command name. This is generally a better resource that the one in windows and is the primary documentation for the system. Use the space bar to page forward in manual. Typing 'b' will backup a page and 'q' will quit the man command. You can even type 'man man' to learn how to use the man command.

[edit] An example

For the copy command something similar to the following output will appear if you type 'help copy' (I have reduced the list of options for this discussion to keep it simple):

COPY [/V] [/Y | /-Y] [/A | /B ] source [/A | /B] destination [/A | /B]

The [], brackets, represent options that are not required while the |, pipe symbol means that the options are mutually exclusive (you can only use one or the other). As can be seen you have two arguments, source and destination, and several options. These mean:

  • /V - verify the file after copying it.
  • /Y - Don't ask if overwriting a destination file.
  • /-Y - Do ask if overwriting a destination file.
  • /A - The file contains ASCII data
  • /B - the file contains Binary data

Normally the contents of the file doesn't matter to copy but the options are there to explicitly tell it due to the fact that Windows can add an end of file marker to ASCII files. There are three places where this option can be entered with the following meanings, use option for both files, use option of source file, use options for destination file.

The destination can be a filename, a pathname, or both.

[edit] Filename

A filename argument specifies the file you want to use. This must be fully specified including any extensions. If the file you want is not in the directory where you are located (Enter the command 'dir' to see what is in the directory and what directory you are in.) then you will have to tell the computer where to find it by specifying a fully qualified pathname\filename like:

"c:\My Documents\eBooks\MyFavorite.txt"

The quotes are required if there are any spaces in the name.

[edit] Pathname

If an argument (filename) is in the current directory its name is all that is required but if it is in some other place on the disk or another disk then a pathname will be need to be added ahead of the filename. Pathnames can also be used ahead of the command itself to force the computer to use a particular version of a command or to find a command that is not in the path list.

Windows uses a backslash (\) to separate the pathnames from the file name and from each other. If there are spaces in the pathname or file name then the complete list should be in quotes. If the object is on another disk then the start of the pathname should include the disk letter followed by a colon.

Unix uses a slash (/), sometimes called a forward slash, to separate the pathnames and file names. Like windows if there are spaces in the name then the whole thing needs to be quoted.

Pathnames are absolute if they begin with a path separator (/ or \). Otherwise they are referenced as starting in your current directory. The current directory can also be specified explicitly using a dot (.). A .. can be used to specify the parent directory.The .. can be repeated to go further up the directory structure as in ../.. (or ..\..).

[edit] Shell

The idea of a shell dates back to the early days (about 1970) when Unix was developed. The modular design of Unix wanted to hide the inner workings of the computer operating system (called the kernel) from user errors that might crash the system. For this reason they designed a shell that surrounded the kernel completely like the shell of an egg. The user enters commands into the shell and then the shell interprets these commands and issues internal commands to the kernel to do the work. The shell provides a defined user interface for the user and intercepted bad information to provide clear messages for the user. There could be multiple shells with each providing a friendly environment for the user.

The simplest shell possible might only be capable of showing a prompt and finding other programs to execute similar to the run... on the start menu. But at the very least it should also be able to display on the screen and change directories. Most real shells can do a whole lot more. A shell also defines the syntax for the command. For example in a Unix shell a / (slash) is used to separate pathnames from each other and filenames while in Windows a \ (backslash) performs this function.

A shell will typically include additional capabilities. These include:

  • Internal commands that are built in to shell itself so it won't have to look for them.
  • Variables that can be set to tell the shell things it might need to know such as where the commands and programs are stored on the system.
  • The ability to display and change variables.
  • The ability to redirect input and output for a particular command.
  • The ability to communicate with the commands to provide arguments and options as input and receive status back from the command as to how well it worked.
  • The ability to communicate messages to the user.
  • The ability to execute scripts or batch files that can contain multiple commands.
  • Some programming like commands to test for things and provide for different results by displaying messages or executing different commands.

[edit] Starting a Shell

In windows the shell is called CMD and may be started from the RUN... menu choice or from the menus themselves if it is setup there. If you use a CMD shell often you may want to make a shortcut on your desktop. Use explorer to drag the shell from C:\WINDOWS\SYSTEM32\CMD.EXE to your desktop. (historic note: Earlier versions of Windows locate this in \WINDOWS or \WINDOWS\SYSTEM and may call it COMMAND.COM which was its original DOS name.)

In Windows if you try and start a command that needs a shell it will automatically start and open a shell window. It will then go away when the command completes. In Unix you start the Window, 'xterm' for example, and your default shell will automatically start within the Window.

Once a shell is open you can type commands at the prompt. When you enter the command line statement the shell will look for the command and then execute it with the argument and options you provided. First it will look and see if it knows how to do the command internally to the shell itself and then if it not found it will look in the locations specified in the PATH variable to find the command. If it is not in the PATH variable the command execution will fail. You can tell it where to find the command by specifying the path ahead of the command when you type it in like you would for a filename.

See Command Prompt Vista/XP/Mac for a visual view of the steps to start a Shell.

[edit] Useful Shell commands

  • 'cd' to change directories. Add the desired directory as an argument for the command. Typing CD by itself in Windows will report the current directory. In Unix use PWD, print working directory, for this information. In Unix typing CD will switch to your home directory.
  • In windows enter 'D:' to change the disk drive partition you are using. Use an appropriate letter for the Drive you want. CD will change the default directory if you add the drive name to the pathname but will not change your location. There is no corresponding Unix command as all disks are reference directly using a directory path.
  • 'set' will list all of the variables known to the shell. Use help (windows) or man (unix) to find what else you can do with it.
  • 'echo' will print out whatever the rest of the line says. This can be used to print out a variable. For example, in windows:
echo &PATH& 

will print out the path variable containing the list of all the locations where commands are located. (Windows also has the PATH command which will do the same thing and can be used to temporarily set the path.) In Unix you would use:

echo $PATH

[edit] Object oriented Shell

The windows shell is also object oriented. This is similar to the GUI interface where you simply click on a file in an explorer window and the system figures out what command to use by looking at the file extension and then opens the appropriate command on the file you clicked.

In a windows shell you can type a filename with its extension instead of a command and if the extension is known the shell will automatically find and execute the command with the filename as an argument. This will work even if the command isn't in the search path.

To see what commands are associated with what extension you can use the 'assoc' command to display them.

[edit] Advanced Features

Command line completion can ease the task of typing in filenames. In Windows you can type a few letters and then hit the tab key. The shell will try and complete the name from the list of files in the directory. If it doesn't choose the one you wanted try hitting the tab key again. Most Unix shells also have command line completion.

History stack can ease the task of repeating things you did before. In Windows the arrow keys can show previous commands that have been used. You can then execute the same command again or edit the line and then execute the modified command line. This can save a lot of typing. Many Unix shells also have a history stack.

Copy and Paste can capture previous commands or data from the screen and use it in the shell or paste it into another window. For windows you can click the right mouse button with the window selected to initiate a menu for copy and paste or you can click the icon in the upper left of the banner and select edit from the menu. To initiate the copy select mark from the menu and then click and hold the left mouse button while dragging over the text. Unix has a similar feature for xterm windows using the middle mouse button.

Input and Output Redirect can be accomplished by using the "<" symbol for input and the ">" for output. This allows using files for input and/or output instead of the keyboard and screen. In some implementations it can allow other devices instead of the defaults. The desired file follows the symbol. The ">>" allows appending to an existing file instead of replacing it which is used to make a log of several commands. The "|", pipe symbol, sends the output to another command as its input. (It must be the last entry of the line for the command and the new command follows the pipe.) This permits stacking several commands together to accomplish the final goal. And finally some shells have a 2> and 2>> output redirect that send error messages independent from regular messages.

Batch Processing allows a shell to execute a shell script as if it were a single command. The user pre-prepares a file containing several commands and then executes the file. The shell reads the file and executes all of the commands in sequence. This can be especially powerful when using "if/then" type statements available in most shells to create a simple (or not so simple) program. In Windows this script will have a .bat (or .cmd) extension while in Unix it must have the execute bit set to be an executable. Since Unix can have multiple shells available there is a feature that permits the file line to select which shell is to be used to execute the script. An example of the first line for a Bourne Shell script is:

#!/bin/sh

Other programs that accept script style input can be used instead of a shell. Perl and Python will both work this way.

[edit] Wild cards

There are times when you want to do a similar thing to several files at once. This is accommodated through the use of wild card characters. For example an * character could be used by itself to many all of the files in the current directory. The character ? can be used to substitute for a single character for example an argument called 'file?' could mean file1, file2 or fileb or any other filename that has one letter after the word file.

Unix and Windows use similar wild card characters but they are handled very differently. In windows the wild card character is passed as part of the filename to the command. The command will have to be coded to understand how to interpret the wild card character. In Unix the wild card characters are handled by the shell. The shell expands the wild card into a list of file names that match the criteria. The commands, on the other hand, are designed to work with a list of file names rather than a single file name with a wild card in it. While, most of the time, the results will be the same there are times when this treatment will change everything. There are some Unix commands that want to handle the wild card themselves. To accommodate this behavior a wild card that is escaped, that is surrounded by quotes or preceded with a \ will be sent unmodified to the command.

The exact meaning of an * is an unlimited number of characters will match. for example a* will match every file name that begins with a no matter how long it is including a itself.

[edit] Useful Commands

A command line interface can provide access to applications but one of it main uses is that of handling utility tasks such as file maintenance. The use is so common that some shells actually include the commands in the shell itself but in others they are simply executables that are themselves files on the disk. (The useful commands that are nearly always present in a shell are covered above). A few such commands include:

Windows Unix explanation
dir ls list the files present in a directory
copy cp copy one file to another location (The name can be changed if desired)
move mv move a file to a different location
ren mv rename a file to a different name
erase rm remove a file (also windows del (delete)

[edit] special characters

Note that in the discussion above there was a wide use of special characters to mean specific things to the shell. The limitation is that these special characters cannot also be used in a filename as their use could and likely would confuse the shell parser. In particular a space is used to separate the various filenames and arguments and thus cannot be a part of the filename for one file. To permit most special characters to be used in a filename the system allows placing a filename inside quotes, " " or ' '. Using quotes will negate the special meaning for the shell and pass the name unmodified to the program that the shell is running. However, that does not mean that all programs can handle a file name so presented. In some cases you could pass a wildcard such a * to a program and it will handle it as a wildcard which could cause a different behavior from the same wildcard that was not quoted.

[edit] eBook Reader

How about a command line eBook Reader? There could be times when it would be useful.

[edit] For more information

In Windows use 'help cmd' for information on the Windows shell and some of the shell commands.

In Unix use the 'man' command, short for manual. Type man followed by the command name for a complete manual on using the command. Popular Unix shells include sh, csh, tcsh, bash, and ksh. (There are others.)

http://en.wikipedia.org/wiki/List_of_Unix_programs

http://en.wikipedia.org/wiki/Win32_console

http://en.wikipedia.org/wiki/Cmd.exe

For more information on command lines see http://en.wikipedia.org/wiki/Command_line

Personal tools
Namespaces

Variants
Actions
Navigation
MobileRead Networks
Toolbox
Advertisement