The language of shells

Unix Insider –

From the end user's perspective, the shell is the most important program on the Unix system because it is the user's interface to the Unix system kernel. The shell reads and interpreting strings of characters and words.

The shells operate in a simple loop:

  1. Accept a command
  2. Interpret the command
  3. Execute the command
  4. Wait for another command

The shell displays a prompt, notifying the user that it is ready to accept a command. It would be nice if you could speak or type instructions into the computer in some form of natural language.

<font face="Courier">OK, Hal. Sort out my correspondence, throw out anything
that is too old, and archive the rest.

Unfortunately, the shell recognizes a very limited set of command words, so the user must offer commands in a way that it understands. This means learning to string odd words and punctuation together.

Each shell command consists of a command name, followed, if desired, by command options and arguments. The command name, options, and arguments are separated by blank space.

The shell is one of many programs that the Unix kernel can run for you. When the kernel is running a program, that program is called a process. The kernel can run the same program many times (one shell for each user), and each running copy of the program is a separate process. Because each user runs a separate copy of the shell, each user is running in his or her own process space.

Many basic shell commands are subroutines that are built in to the shell program. The

<font face="Courier">echo</font>
command is almost always built in to a shell.

<font face="Courier">$ echo "Hello, Hal"
Hello Hal

Commands not built in to the shell require that the kernel start another process in order to run.

When you execute a command that is not built in to a shell, the shell asks the kernel to create a new subprocess (or child process) to perform the command. The child process exists just long enough to execute the command. The shell waits for the child process to finish before accepting the next command.

The basic form of a Unix command is:

<font face="Courier">command name [-options] [arguments] 

The square brackets signify parts of the command that may be omitted.

The command name is the name of a built-in command or a separate program you want the shell to execute. The command options, usually indicated by a dash, allow you to alter the behavior of the command. The arguments are the names of files, directories, or programs that the command needs to access.

<font face="Courier">ls -l /home/mjb


<font face="Courier">ls</font>
command is usually a separate program rather than a built-in command. The command above will get you a long listing of the contents of the
<font face="Courier">/home/mjb</font>
directory. In this example,
<font face="Courier">ls</font>
is the command name,
<font face="Courier">-l</font>
is an option that tells
<font face="Courier">ls</font>
to create a long, detailed output, and
<font face="Courier">/home/mjb</font>
is an argument naming the directory that
<font face="Courier">ls</font>
is to list.

The Unix shell is case sensitive, and most Unix commands are lower case.

Some of the more popular shells are

<font face="Courier">sh</font>
(the Bourne shell),
<font face="Courier">ksh</font>
(the Korn shell),
<font face="Courier">csh</font>
(the C shell),
<font face="Courier">bash</font>
, (the Bourne Again shell),
<font face="Courier">pdksh</font>
(the Public Domain Korn shell), and
<font face="Courier">tcsh</font>
(the Tiny C shell).

You can frequently identify your shell by typing:

<font face="Courier">echo $SHELL

Unix recognizes certain special characters as command directives. If you use a special character in a command, make sure you understand what it does. The special characters are

<font face="Courier"> / < > ! $ % ^ & * | { } ~ </font>
<font face="Courier"> ;</font>
. When naming files and directories on Unix, it is safest to only use numerals, upper and lower case letters, and the period, dash, and underscore characters.

A Unix command line is a sequence of characters in the syntax of the target shell language. Of the characters in a command line, some are known as metacharacters. Metacharacters have a special meaning to the shell. The metacharacters in the Korn shell are:

  • <font face="Courier">;</font>
    -- Separates multiple commands on a command line
  • <font face="Courier">&</font>
    -- Causes the preceding command to execute asynchronously (as its own separate process so that the next one does not wait for it to complete)
  • <font face="Courier">()</font>
    -- Enclose commands that are to be launched in a separate shell
  • <font face="Courier">|</font>
    -- Pipes the output of the command to the left of the pipe to the input of the command on the right of the pipe
  • <font face="Courier">></font>
    -- Redirects output to a file or device
  • <font face="Courier">>></font>
    -- Redirects output to a file or device and appends to it instead of overwriting it
  • <font face="Courier"><</font>
    -- Redirects input from a file or device
  • <font face="Courier">newline</font>
    -- Ends a command or set of commands
  • <font face="Courier">space</font>
    -- Separates command words
  • <font face="Courier">tab</font>
    -- Separates command words

Some metacharacters can be used in combinations, such as

<font face="Courier">||</font>
<font face="Courier">&&</font>
, and
<font face="Courier">>></font>
. With these metacharacters you can define a command-line word, which is a sequence of characters separated by one or more nonquoted metacharacters.

To access the online manuals, use the

<font face="Courier">man</font>
command, followed by the name of the command you need help with. For instance, to see the manual for the
<font face="Courier">ls</font>
command, enter:

<font face="Courier">man ls
ITWorld DealPost: The best in tech deals and discounts.
Shop Tech Products at Amazon