Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Reserved words

Some words have special meaning in shell syntax. These reserved words must be quoted to use them literally. The reserved words are:

  • !Negation
  • { – Start of a grouping
  • } – End of a grouping
  • [[ – Start of a double bracket command
  • ]] – End of a double bracket command (since 3.3.0)
  • caseCase command
  • do – Start of a loop or conditional block
  • done – End of a loop or conditional block
  • elif – Else if clause
  • else – Else clause
  • esac – End of a case command
  • fi – End of an if command
  • forFor loop
  • functionFunction definition
  • ifIf command
  • in – Delimiter for a for loop and case command
  • namespace – Namespace declaration (since 3.3.0)
  • select – Select command (since 3.3.0)
  • then – Then clause
  • untilUntil loop
  • whileWhile loop

The reserved words [[, ]], function, namespace, and select are recognized but not yet implemented. Using these reserved words will result in a syntax error.

Additionally, the POSIX standard allows for the following optional reserved words:

  • time – Time command
  • Any words that end with a colon (:)

These words are not reserved in yash-rs now, but may be in the future. (Since 3.3.0) The portable option rejects a command name ending with a : (the lone : colon built-in is exempt).

Where are reserved words recognized?

Reserved words are recognized in these contexts:

  • As the first word of a command
  • As a word following any reserved word other than case, for, or in
  • in as the third word in a for loop or case command
  • do as the third word in a for loop

As an extension, yash-rs additionally recognizes a reserved word immediately after a subshell or a redirection, which POSIX does not, so such scripts are not portable. (Since 3.3.0) The portable option rejects a reserved word in this position; insert ; or a newline before it.

Examples

This example uses the reserved words for, in, do, and done in a for loop:

$ for i in 1 2 3; do echo $i; done
1
2
3

In the following example, {, do, and } are not reserved words because they are not the first word of the command:

$ echo { do re mi }
{ do re mi }

Reserved words are recognized only when they appear as a whole word. In this example, { and } are not reserved words because they are part of {echo and Hello}:

$ {echo Hello}
error: cannot execute external utility "{echo"
 --> <stdin>:1:1
  |
1 | {echo Hello}
  | ^^^^^ utility "{echo" not found

To use { and } as reserved words, write them as separate words:

$ { echo Hello; }
Hello

Per the extension described above, a reserved word is also recognized right after a subshell or a redirection. Here, the } closes the grouping even though it immediately follows the subshell ( … ):

$ { ( echo Hello ) }
Hello

This is not portable; inserting a separator, as in { ( echo Hello ); }, makes it portable.