Command line
From MobileRead
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 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.)
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 XP 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 window with a prompt at the bottom. To end the CMD window session you can enter the command 'exit' at the prompt.
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 finished it would 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.
For more information on command lines see http://en.wikipedia.org/wiki/Command_line
[edit] Syntax
Every shell will have a defined syntax. That is, it will have a set of rules that it will use to parse the command line and figure out what to do. The syntax starts with the command and then has arguments and possibly switches, also known as options, to help guide the command.
[edit] Filenames
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\file.txt"
If the filename or any part of the pathname has any spaces in it you will need to enclose the whole argument in quotes so that the shell doesn't get confused when trying to parse the line.
Unix is similar but the / (slash) is used as a separator and there is no drive letter. (Unix mounts drives as using directory names.)
Many commands that are GUI based can still be started from a commandline (or a shortcut) and provided with a filename. For example:
notepad file.txt
will open notepad with the named file. If it does not exist notepad can create it.
[edit] Options or Switches
Options change the default behavior for a command. In Unix an option is preceded by a - (hyphen). In Windows an option is preceded with a / (slash). If the windows command originally came from Unix it may use a - even in windows.
An example of an option might be: In windows
copy /y file.txt file.bak
would tell the copy command to perform the copy without prompting you to overwrite an existing file. The default behavior of copy in windows is to ask. In Unix:
cp -i file.txt file.bak
would cause the command to ask before overwriting as the default behavior for Unix is not to ask.
Recent additions to Unix include a rewrite of many of the traditional Unix commands and some new ones that are freely available from GNU (Gnu's Not Unix). These commands are normally supplied in Linux and downloadable in versions that will run under windows. In addition to the hyphen options they support a "long option" using two hyphens in a row (--). These options are usually whole words instead of the single letter abbreviations.
[edit] Syntax help
Every command can have very unique options to it can be difficult to keep track of them. Most commands will give you a list of their options if you ask them nicely.
For windows, the /? can often be used to request the syntax of a command. In addition there is a help command that can be used to get syntax for some of the commmands. Type 'help' on a line by itself to get a list of commmands that it knows about and then use 'help command-name' to get the syntax and explanation.
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.
Unix commands often have a help syntax mode as well. If long-options are supported the option '--help' will usually work. Otherwise try -? or -h. In some cases no argument at all will show the syntax. If all of this fails use the 'man' command to bring up the manual on the command.
[edit] Shell
The idea of a shell dates back to the early days 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 you can find it. 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 the 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.
[edit] Useful Shell commands
- 'cd' to change directories. Typing CD by itself in Windows will report the current directory. In Unix use PWD, print working directory, for this information.
- 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.
- '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. 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.
[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 Unx 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 extension while in Unix it must have been set to be 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 except script style input can be used instead of a shell. Perl and Python will both work this way.
[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

