Introduction to Unix |
|
![]() |
![]() |
2.1. Types of Commands¶
External program on disk which could be
- a binary executable (written in C, C++).
- a script file (like a shell or perl script).
Internal command of the shell like cd, pwd, etc.
Note
Most commands exist as disk files. They can be executed both from the shell and shell scripts. Shells today are feature-rich and some of the frequently used commands are now built into the shell. The shell looks in its own arsenal before it searches the disk.
There are some commands which exist both as external and internal commands. Consider the echo and pwd commands both of which were originally external commands but now are built into the shell. Unix systems still ship these external commands but they are rarely used and provide backward compatibility for programs that use them using a pathname.
Even though the shell ignores the disk version if an alias or built-in is available, sometimes you may need to use the disk version. There are two methods:
- Use a pathname that specifies where the command is located.
- Precede a \ before the command name if a built-in or alias exists for the command name. For instance, use \cp if cp has been converted to an alias by the user and you want to invoke the disk version.
It’s important for the user to know which version of a command actually runs, so she must execute the alias command to check whether an alias for the command is available. Use the type command to check whether the command is a shell built-in.
There is one program, cd, that is built into the shell but is not available as a separate disk program. The cd command can never work as an external command.
2.2. Structure of a Command¶
Consider, for example:
ls -l -u -t chap01
- Command filenames need no specific extensions.
Note
Nothing prevents us from having commands with extensions. In fact, we do use .sh and .pl for shell and perl scripts, respectively, but that is for our own convenience.
- Command and arguments must be separated by whitespace.
Note
Whitespace is defined as a contiguous string of tabs, spaces and newlines.
Many options have single letter and spelled out word equivalents.
-a
may be equivalent to--all
Generally possible to combine multiple options into a single one.
ls -l -u -t
is equivalent tols -lut
Order of options is generally not important.
ls -lut
is equivalent tols -utl
The components of an entered command may be categorized into one of four types: command, option, option argument and command argument.
-
command
The program or command to run. It is the first word in the overall command.
-
option
An option to change the behavior of the command. The available options are described in the command’s manual page.
-
option argument
Some options have their own arguments. For instance, sort has a -t option to specify a delimiter (
-t :
), and tar requires the -f option to be used with a filename (-f /dev/fd0
).
-
command argument
An argument to give the program some additional information, such as the name of a file or a string to search for.
-
The specification for the allowed usage of each command is documented in the command’s manual page according to the Backus–Naur Form (BNF). - See Understanding a man Page and Figure A-5, pg 598.
Sequence Meaning [ strings ] Strings may appear zero or one times. { strings }* Strings may appear zero or more times. { strings }+ Strings may appear one or more times. string1|string2 string1 or string2 may appear. -optionlist Zero or more single letter options may follow a dash. Examples:
$ ls $ ls -la $ ls -la m* $ lpr -Pspr -n 3 proposal.ps