Where is "key=value bash-script" usage documented?

Viewed 56

I often see key=value bash-script to pass variables to a bash script. Here is an example:

$ echo $0
-bash
$ cat foo.sh
#!/usr/bin/env bash
echo "key1: $key1"
$ key1=value1 ./foo.sh
key1: value1

I have checked Bash Reference Manual. But I can't a description related to this usage.

2 Answers

Section 3.7.4 "Environment"

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

Apparently the online version of the documentation fails to describe the syntax of a simple command completely. It reads:

3.2.1 Simple Commands

A simple command is the kind of command encountered most often. It’s just a sequence of words separated by blanks, terminated by one of the shell’s control operators (see Definitions). The first word generally specifies a command to be executed, with the rest of the words being that command’s arguments.

There is some information about the variable assignments that may precede a simple command on the section Simple Commands Expansion:

3.7.1 Simple Command Expansion

When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right.

  • The words that the parser has marked as variable assignments (those preceding the command name) and redirections are saved for later processing.

...

  • The text after the = in each variable assignment undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable.

The manual page bundled with the shell seems to be better at this. It says (the emphasis is mine):

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator.

You can always read the documentation of bash (and of any other CLI program installed on your computer) using man.

Type man bash on your terminal and use the usual keys (<spacebar>, b, /, q etc.) to navigate in the document. Behind the scenes, man uses your default pager program (which is, most probably, less) to put the information on screen.

Related