5

What is the reason behind enclosing all lines in a shell script in curly braces?

e.g. the entire content of this script is enclosed in braces:

#!/bin/sh

{
set -e

LATEST="v0.3.5"
DGOSS_VER=$GOSS_VER

if [ -z "$GOSS_VER" ]; then
    GOSS_VER=${GOSS_VER:-$LATEST}
    DGOSS_VER='master'
fi
GOSS_DST=${GOSS_DST:-/usr/local/bin}
INSTALL_LOC="${GOSS_DST%/}/goss"
DGOSS_INSTALL_LOC="${GOSS_DST%/}/dgoss"
touch "$INSTALL_LOC" || { echo "ERROR: Cannot write to $GOSS_DST set GOSS_DST elsewhere or use sudo"; exit 1; }

arch=""
if [ "$(uname -m)" = "x86_64" ]; then
    arch="amd64"
else
    arch="386"
fi

url="https://github.com/aelsabbahy/goss/releases/download/$GOSS_VER/goss-linux-$arch"

echo "Downloading $url"
curl -L "$url" -o "$INSTALL_LOC"
chmod +rx "$INSTALL_LOC"
echo "Goss $GOSS_VER has been installed to $INSTALL_LOC"
echo "goss --version"
"$INSTALL_LOC" --version

dgoss_url="https://raw.githubusercontent.com/aelsabbahy/goss/$DGOSS_VER/extras/dgoss/dgoss"
echo "Downloading $dgoss_url"
curl -L "$dgoss_url" -o "$DGOSS_INSTALL_LOC"
chmod +rx "$DGOSS_INSTALL_LOC"
echo "dgoss $DGOSS_VER has been installed to $DGOSS_INSTALL_LOC"
}

Shellcheck.net deems the script valid with or without the curly braces.

don_crissti
  • 79,330
  • 30
  • 216
  • 245
Zlemini
  • 591
  • 5
  • 10
  • 1
    The script you linked to does *not* have every line enclosed in curly braces. Could you please be more specific which uses you are asking about? – Wildcard Mar 16 '18 at 23:31
  • It might be you're looking for https://unix.stackexchange.com/q/122845/135943, but it's hard to tell. – Wildcard Mar 16 '18 at 23:32
  • Or possibly https://unix.stackexchange.com/a/306141/117549 or https://unix.stackexchange.com/q/164482/117549 or https://unix.stackexchange.com/a/148480/117549 – Jeff Schaller Mar 16 '18 at 23:45
  • 2
    Looks like it's from https://github.com/aelsabbahy/goss/commit/bff9fbc24ca76df4c30a67e155bd7836e43062b9 ? I don't see any obvious reason to do that. Maybe ask the author? – Mikel Mar 16 '18 at 23:47
  • A possible reason: [How to read the whole shell script before executing it?](//unix.stackexchange.com/q/331837) – Stéphane Chazelas Mar 17 '18 at 08:53

1 Answers1

5

There is no obvious reason to do this. The curly braces is a grouping construct and the commands within them will be executed in the same environment as the rest of the script.

Had it been an ordinary parenthesis, then it would have been a sub-shell (a separate environment from the rest of the script), but in this instance that too would not have made much difference.

One possible reason for this is that it would enable the author to redirect all output from any command within the { ... } to some specific place, as in

{ ...some commands...; } >somefile

but this is obviously not done here.

With parenthesis,

( ...some commands... )

the author would have been able to set shell options and create local variables that don't affect the rest of the script.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • 2
    A common reason for that is to guard against the script being overwritten while it's running (though you'd want to terminate it with `}; exit`). That's also why some like to place their whole script in a `main` function. – Stéphane Chazelas Mar 17 '18 at 08:33
  • 2
    See [How to read the whole shell script before executing it?](//unix.stackexchange.com/q/331837) – Stéphane Chazelas Mar 17 '18 at 08:36