Shell Components   «Prev  Next»
Lesson 3Components of shell program: external and built-in commands
ObjectiveBasic built-in commands/ how they differ from external commands.

External and built-in Commands

Define basic built-in commands and how they differ from external commands. The UNIX shell is a program that interacts with users through a command-line interface. When you enter a command, the shell processes your entry and acts accordingly. Some of the commands that you enter at a command line are performed by the shell itself, while others initiate separate programs that the shell starts. The two types of commands are used the same way, but processed differently. Two simple examples that illustrate the difference are the cd and ls commands.

Examples of the two command types

  1. When you enter the cd command, the shell itself acts on the command, changing your current working directory. Various options may be supported with the cd command, depending on the shell you are using. No additional programs are started to process your command. This type of command is called a built-in command.
  2. When you enter the ls command, the shell starts a program called ls, usually located in a directory like
    /bin or
    /usr/bin
    

    Any options that you have entered are passed to the ls program. The shell may interpret some of your options as it passed them to the ls program, but it does not act on them. That is left for the ls program. The ls program runs as a separate UNIX process[1]. This type of command is called an external command. The actions of external and built-in commands are illustrated in the following diagram:

External and built-in commands
  1. built-in commands : cd,
  2. External command: ls

Relationship of Commands to Shell Programming

Most of the commands you commonly use in UNIX are external commands, they require that the shell run a separate program. Some commands (like cd) are built-in. They are part of the shell. Often the built-in commands are those used only within shell scripts. Built-in commands are specific to one shell. By knowing which commands in your shell scripts are built-in, you can anticipate possible problems moving a script to a different shell. Because external commands are not part of any shell, their actions are not affected by which shell you are using. In the next several lessons, you will learn about components of shell scripts. These will be discussed in greater detail in later modules of the course, but this module will introduce you to the basic concepts.

Built-in and External Commands

The following table shows a few of the built-in and external commands that are often used in shell scripts. You will learn what each command does in the lessons and modules to come. This table is intended to help you recognize which commands are external and which are built in to the shell. The Emacs variable remote-shell-program contains the path to any desired program for invoking a remote shell. Simply redefine it to be the full path to your ssh executable. Also, the rlogin package, rlogin.el, defines a variable rloginprogram you can redefine to use slogin.

Shell Programming

  1. basename: Print the last component of a pathname, optionally removing a suffix.
  2. dirname: Print all but the last component of a pathname.
  3. echo: Repeat command-line arguments on the output.
  4. expr: Perform arithmetic and comparisons.
  5. id: Print user and group ID and name information.
  6. line: Read a line of input.
  7. printf: Format and print command-line arguments.
  8. sleep: Pause during processing.
  9. test: Test a condition.


Running Commands

Bash's core ability is to run commands on your system. Let us try a quick "Hello World" example. In a bash shell, the echo command displays text to the screen, like so:
$ echo "Hello World"

Enter this on the bash command line and you’ll see the words Hello World displayed onscreen. This line of code runs the echo command that is stored in your standard bash library. The directories that bash will search for these standard commands are stored in an environment variable called PATH. You can use echo with the PATH variable to see its contents in the listing below. shows.
$ echo $PATH
/Users/ggould/.rvm/gems/ruby-2.1.5/bin:/Users/ggould/.rvm/gems/ruby-2.1.5@
global/bin:/Users/ggould/.rvm/rubies/ruby-2.1.5/bin:/usr/local/bin:/usr/bin:/
bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:/Users/ggould/.rvm/bin

[1]Process: A task within the UNIX kernel; an independent program.