Bash – Differences Between Keyword, Reserved Word, and Builtin

bash

From Make bash use external `time` command rather than shell built-in, Stéphane Chazelas wrote:

There is no time bash builtin. time is a keyword so you can do for instance time { foo; bar; }

We can verify it:

$ type -a time
time is a shell keyword
time is /usr/bin/time

It doesn't show that time can be a builtin command.

  1. What is the definition of a "keyword"?
  2. is "keyword" the same concept as "reserved word" in Bash Reference
    Manual?

    reserved word

    A word that has a special meaning to the shell. Most reserved words introduce shell fl ow control constructs, such as for and
    while.

  3. Is a keyword necessarily not a command (or not a builtin command)?

    As a keyword, is time not a command (or not a builtin command)?

    Based on the definitions of keyword and of builtin, why is time
    not a builtin but a keyword?

  4. Why "you can do for instance time { foo; bar; }" because "time is a keyword"?

Best Answer

Keyword, reserved word, and builtin are all the "first word" of a Simple command. Could be placed in two groups: Keyword and Builtin. The two are mutually exclusive. A word (token) can be either a Keyword or a builtin, but not both.

Why the "first word"

From the POSIX definition of "Simple command"(emphasis mine):

A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.

2.- The words that are not variable assignments or redirections shall be expanded. If any fields remain following their expansion, the first field shall be considered the command name and remaining fields are the arguments for the command.

After that "first word" has been identified, and after is has been expanded (by an alias, for example) the final word is "the command", there could be only one command in each line. That command word could be a Built-in or a keyword.

Keyword

Yes, a keyword is a "reserved word". Load "man bash" and search for keyword. (or just execute this command: LESS=+/'keyword' man bash. The first hit on search say this:

keyword Shell reserved words.

It happens in the completion section, but is quite clear.

Reserved Words

In POSIX, there is this definition of "Reserved Words" and some description of what Reserved Words do.

But the bash manual has a better working definition.
Search for "RESERVED WORDS" (LESS=+/'RESERVED WORDS' man bash) and find this:

RESERVED WORDS Reserved words are words that have a special meaning to the shell.
The following words are recognized as reserved when unquoted and either the first word of a simple command or the third word of a case or for command:

! case  do done elif else esac fi for function if
in select then until while { } time [[ ]]

Builtin

It is not defined in the bash manual, but it is quite simple:

It is a command that has been implemented inside the shell for UN-avoidable needs of the shell (cd, pwd, eval), or speed in general or to avoid conflicting interpretations of external utilities in some cases.

Time is a keyword.

why is time not a builtin but a keyword?

To allow the existence of a command as the second word.

It is similar as how an if ... then .... fi allow the inclusion of commands (even compound commands) after the first key-word if. Or while or case, etc.