For many years before the invention of graphical user interfaces, such as X-Windows (the X Windowing System, for purists), the only way to input commands to the operating system was through a command line interpreter, or shell. In fact, shells themselves were thought of as wondrous things during the early days of computers because prior to them, users had no direct way to interact with the operating system.
Most shells, be they under DOS, UNIX, VMS, or other operating systems, have the same input characteristics. To get the operating system to do anything, you must give it a command. Some commands, such as the date command under UNIX, do not require anything else to get them to work. If you type in date and press Enter, that's what appears on your screen: the date.
Some commands need something else to get them to work: an argument. Some commands, like mkdir (used to create directories), work with only one argument, as in mkdir directory_name. Others, like cp (to copy files), require multiple arguments, as in
cp file1 file2
In many cases, you can pass flags to commands to change their behavior. These flags are generally referred to as options. For example, if you wanted to create a series of sub-directories without creating every one individually, you could run mkdir with the -p option, like this:
mkdir -p one/two/three/four
In principle, anything added to the command line after the command itself is an argument to that command. The convention is that an option changes the behavior, whereas an argument is acted upon by the command. Let's take the mkdir command as an example:
mkdir dir_name
Here we have a single argument which is the name of the directory to be created. Next, we add an option:
mkdir -p sub_dir/dir_name
The -p is an option. Using the terminology discussed, some arguments are optional and some options are required. That is, with some commands you must always have an option, such as the tar command. Some commands don't always need to have an argument, like the date command.
Generally, options are preceded by a dash (-), whereas arguments are not. I've said it before and I will say it again, nothing is certain when it comes to Linux or UNIX, in general. By realizing that these two terms are often interchanged, you won't get confused when you come across one or the other. I will continue to use option to reflect something that changes the command's behavior and argument to indicate something that is acted upon. In some places, you will also see arguments referred to as "operands". An operand is simply something on which the shell "operates", such as a file, directory or maybe even simple text.
Each program or utility has its own set of arguments and options, so you will have to look at the man-pages for the individual commands. You can call these up from the command line by typing in
man
where
Arguments (whether they are options or operands) which are enclosed in square brackets ([ ]) are optional. In some cases, there are optional components to the optional arguments, so you may end up having brackets within brackets.
An ellipsis (...) Indicates that the preceding arguments can be repeated. For example, the ls command can take multiple file or directory names as arguments as well as multiple options. Therefore, you might have a usage message that looks like this:
ls [OPTION] ... [FILE] ...
This tells us that no options are required, but if you wanted you could use multiple options. It also tells us that no file name is required, but if you wanted you could use multiple ones.
Words that appeared in angle brackets (< >) or possibly in italics in the printed form, indicate that the word is a place holder. Like in the example below:
man
Many commands require that an option appear immediately after the command and before any arguments. Others have options and arguments interspersed. Again, look at the man-page for the specifics of a particular command.
Often, you just need a quick reminder as to what the available options are and what their syntax is. Rather than going through the hassle of calling up the man-page, a quick way is to get the command to give you a usage message. As its name implies, a usage message reports the usage of a particular command. I normally use -? as the option to force the usage message, as I cannot think of a command where -? is a valid option. Your system may also support the --help (two dashes) option. More recent versions of the various commands will typically give you a usage message if you use the wrong option. Note that fewer and fewer commands support the -?.
To make things easier, the letter used for a particular option is often related to the function it serves. For example, the -a option to ls says to list "all" files, even those that are "hidden". On older versions of both Linux and Unix, options typically consisted of a single letter, often both upper and lowercase letters. Although this meant you could have 52 different options it made remembering them difficult, if they were multiple functions that all began with the same letter. Multiple options can either be placed separately, each preceded by a dash, or combined. For example, both of these commands are valid and have the exact same effect:
ls -a -l
ls -al
In both cases you get a long listing which also included all of the hidden files.
Newer versions of commands typically allow for both single letter options and "long options" which use full words. For example, the long equivalent of -a would be --all. Note that the long options are preceded with two dashes because it would otherwise be indistinguishable from the -a followed by two -l options.
Although it doesn't happen too often, you might end up with a situation where one of the arguments to your command starts with a dash (-), for example a file name. Since options typically start with a dash, the shell cannot figure out that it is an argument and not a long line of options. Let's assume that some application I had created a file called "-jim". If I wanted to do a simple listing of the file, I might try this:
ls -jim
However, since the shell first tries to figure out what options are being used before it shows you the listing, it thinks that these are all options and gives you the error message:
ls: invalid option -- j
Try `ls --help' for more information.
You can solve this problem with some commands by using two dashes to tell the command that what follows is actually an argument. So to get the listing in the previous example, the command might look like this:
ls -- -jim
No comments:
Post a Comment