Senthil Kumaran
Senthil Kumaran

Reputation: 56941

Bash multi-line with `{` and `}` syntax

I realized in bash, and perhaps in other shells too, multiline commands can be pasted within

{

# some bash command here

}

I realize this has to be newline separated

For e.g.

$ { date }

> -bash: syntax error: unexpected end of file
$ {
> date
> }
Sat Aug 24 06:02:03 PDT 2019

Upvotes: 0

Views: 49

Answers (1)

chepner
chepner

Reputation: 532043

{...} is one of several compound commands described in the man page. Neither { nor } is a reserved keyword; they are only treated specially when they appear in the command position of a simple command. For that reason, the last command in the group has to be properly terminated (either with a newline or a semicolon), so that } isn't treated simply as another argument of the last command.

$ { date; }
Sat Aug 24 06:02:03 PDT 2019

From the man page:

   Compound Commands
       A  compound command is one of the following.  In most cases a list in a
       command's description may be separated from the rest of the command  by
       one  or  more  newlines, and may be followed by a newline in place of a
       semicolon.

       [...]

       { list; }
              list is simply executed in the current shell environment.   list
              must  be  terminated with a newline or semicolon.  This is known
              as a group command.  The return status is  the  exit  status  of
              list.   Note that unlike the metacharacters ( and ), { and } are
              reserved words and must occur where a reserved word is permitted
              to  be  recognized.   Since they do not cause a word break, they
              must be separated from  list  by  whitespace  or  another  shell
              metacharacter.

       [...]

Upvotes: 1

Related Questions