Table of Contents

How do you define an Anonymous Block of Code in Bash?

About

This page shows you how to define a block of code in bash

A non-anonymous block is a function.

What is the syntax?

The block syntax is:

{

   # your code
   
}

Example

Redirect Stdout

Redirect Stdout with a block

{
    cat <<-EOH
       #
       # THIS IS A comment
       #
    EOH
    echo Another
} > "/path/to/file"

Can I use the subshell syntax?

You can use the subshell syntax but you will not get any return status (meaning that you can't exit)

(

   # your code
   
)