chenzhongpu
chenzhongpu

Reputation: 6871

Why can the multiple assignments be separated by space in Shell

In shell, why can these two assignments be separated by space?

foo="foo" bar="bar"

As we know, commands separated by a ‘;’ are executed sequentially. What about the space?

Upvotes: 2

Views: 51

Answers (2)

janos
janos

Reputation: 124724

The SIMPLE COMMAND EXPANSION section of man bash explains the syntax:

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

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

       2.     The words that are not variable assignments or redirections  are
              expanded.   If  any words remain after expansion, the first word
              is taken to be the name of the command and the  remaining  words
              are the arguments.

       3.     Redirections are performed as described above under REDIRECTION.

       4.     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  vari-
              able.

       If no command name results, the variable assignments affect the current
       shell environment.  Otherwise, the variables are added to the  environ-
       ment  of the executed command and do not affect the current shell envi-
       ronment.  If any of the assignments attempts to assign  a  value  to  a
       readonly  variable,  an error occurs, and the command exits with a non-
       zero status.

       If no command name results, redirections  are  performed,  but  do  not
       affect  the  current shell environment.  A redirection error causes the
       command to exit with a non-zero status.

       If there is a command name left after expansion, execution proceeds  as
       described  below.   Otherwise, the command exits.  If one of the expan-
       sions contained a command substitution, the exit status of the  command
       is  the  exit  status  of  the last command substitution performed.  If
       there were no command substitutions, the command exits with a status of
       zero.

Point 1 clearly allows multiple variable assignments before a command name.

The paragraphs after the points clearly allow assignments without any command name.

Upvotes: 2

Igor Chubin
Igor Chubin

Reputation: 64603

It is not separated independent commands; actually it is not commands at all.

It's just syntax of a command line: you can set some variables before running a command. The variables are set only for this command.

var1=val1 var2=val2... cmd

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

Upvotes: 2

Related Questions