General feature of a command:
A UNIX command consists of a single word generally using alphabetic characters. Since the designers created UNIX for their own use, they tried to ensure that a minimum number of keystrokes achieved the maximum amount of work. That is why the original UNIX commands are seldom more than four characters long. You have already used some of them – ls, cat, who etc. some modern commands are long words and occasionally contain a numeral or an underscore.
As you know, commands are essential files. These files are stored in folders which better known as directories. For instance, the ls command is also a program represented by a file that located in the directory /bin.
A UNIX command file does not need to have a special extension like .exe or .com though you can provide one if you want to. There is hardly a practical restriction on length either; a command can be up to 255 characters. Other UNIX system, however, have 14 characters long.
The PATH; locating command:
How does UNIX know whether a command is legitimate or not? When you enter a command, the system searches for its file in certain specified directories. If it finds in file in one of these directories, it executes it; otherwise it flashed the message like “Bad command” and so on.
UNIX functioning is controlled by a number of variables. UNIX obtains the list of directories that has to be searched from one of its variables – the one names PATH. If you evaluate the value of this variable, you will find a directory list separated by colons:
$echo $PATH
/bin : /usr/bin : usr/z116/bin : /oracle/bin:.
There are four directories in this list, and when you issue a command, the system will search this list in the sequence specified to locate and execute it. This means that it will first search in /bin then in /usr/bin and so on.
But in which directory the command is located is find with the help of type command:
$type ls
ls is /bin/ls
ls is located in /bin directory and because /bin is also a component of the value of the PATH variable, the system locates it easily and executes it.
Command structure:
The structure of a command is given below:
The entire command here has five words separated by spaces. The first word “ls” is actually the command ; the other words are called arguments. The ls command here has four arguments.
Commands and arguments are separated by any number of spaces and tabs together form what is known as whitespace.
Here you can see in the diagram that there are lots of whitespaces between the command and the arguments. The UNIX system processes a special mechanism of compressing these multiple consecutive spaces or tabs into a single space.
Internal and external commands:
The agency that actually does all this work is known as the shell. This is a special command that starts running the moment you log in. the shell takes the command that you enter as its input and looks at its own PATH variable to find out where it is located.
Since ls command is a file having an independent existence in the /bin directory , it is called an external command. Most commands are external in nature. But there are some that are not really any where or not executed even if they are found. Take for instance the echo command
$type echo
Echo is a shell built in
When you type echo, the system (rather the shell) would not look in its PATH to locate it (even if it can find it in /bin). Rather it will execute it from its own set of built in commands that are not stored as separated files. These built in commands, of which echo is a member, are known as internal commands. The type command itself is a shell built in.
Options and filenames:
As you can see in the above example that the following command consist of four arguments. Out of the four the two are started with “–“ sign, these two are called the options. Options are used to make a command more flexible. The other two arguments “note1” and “note2” are called the file names.
“The command with its arguments and options is entered in one line that is referred as the command line.”
This line is considered complete only after user has hit “Enter Key”. The complete line is then fed to the shell as its input for interpretations and executions.
Combining option:
Unix is a such kind of operating system that provide you a facility by which you can combine the multiple options of a command in a single command line.
For example we have a command called “ls” and its option like “ –a” , “–t”, “–l”. Such command can be written in two ways like:
$ ls –a –t –l ------------------------ cmd 1------------------------cmd 2
And
$ ls –atl
Note: Here “– l” option provides most details of the file attributes. “ – t” option sort the files as per the time of modification and “ – a” option includes the hidden files of the directory or system.
Moreover we can write down these options in any combinations like:
$ ls –alt
$ ls –tal and so on….
Each and every time you get the same output.
Flexibility in command usage:
Combining commands: UNIX and Linux are such kind of operating system that provide the facility to combine the multiple commands in a single command line. This can be done with the help of “Metacharacters”. For example I want to combine the option the command like “who” and “ls”. These commands can be combined with the help of ; Metacharacter.
$ who; ls –l note1here the “ls” command is executed after “who” command
Here the ; is the special character that the shell understand. When the command contains this character, the shell understands that the commands on either side of it need to be processed separately.
A command line can overflow: As you know that the width of a terminal is 80 characters. But the number of characters in a single command line is up to 255. This means your single command line can be written in two or more lines of a terminal. To make two or more than two lines command line can be made a single line command by using the “Secondary prompt (>)” option.
For example : the “echo command”
$ echo “this is
> a three line
> text message.”
Output: this is
a three line
text message.