Why enclose a shell script in curly braces?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












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"


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







share|improve this question


















  • 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 at 23:31










  • It might be you're looking for unix.stackexchange.com/q/122845/135943, but it's hard to tell.
    – Wildcard
    Mar 16 at 23:32










  • Or possibly unix.stackexchange.com/a/306141/117549 or unix.stackexchange.com/q/164482/117549 or unix.stackexchange.com/a/148480/117549
    – Jeff Schaller
    Mar 16 at 23:45







  • 2




    Looks like it's from github.com/aelsabbahy/goss/commit/… ? I don't see any obvious reason to do that. Maybe ask the author?
    – Mikel
    Mar 16 at 23:47










  • A possible reason: How to read the whole shell script before executing it?
    – Stéphane Chazelas
    Mar 17 at 8:53














up vote
3
down vote

favorite












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"


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







share|improve this question


















  • 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 at 23:31










  • It might be you're looking for unix.stackexchange.com/q/122845/135943, but it's hard to tell.
    – Wildcard
    Mar 16 at 23:32










  • Or possibly unix.stackexchange.com/a/306141/117549 or unix.stackexchange.com/q/164482/117549 or unix.stackexchange.com/a/148480/117549
    – Jeff Schaller
    Mar 16 at 23:45







  • 2




    Looks like it's from github.com/aelsabbahy/goss/commit/… ? I don't see any obvious reason to do that. Maybe ask the author?
    – Mikel
    Mar 16 at 23:47










  • A possible reason: How to read the whole shell script before executing it?
    – Stéphane Chazelas
    Mar 17 at 8:53












up vote
3
down vote

favorite









up vote
3
down vote

favorite











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"


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







share|improve this question














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"


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









share|improve this question













share|improve this question




share|improve this question








edited Mar 16 at 23:52









don_crissti

46.4k15123153




46.4k15123153










asked Mar 16 at 23:24









Zlemini

1736




1736







  • 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 at 23:31










  • It might be you're looking for unix.stackexchange.com/q/122845/135943, but it's hard to tell.
    – Wildcard
    Mar 16 at 23:32










  • Or possibly unix.stackexchange.com/a/306141/117549 or unix.stackexchange.com/q/164482/117549 or unix.stackexchange.com/a/148480/117549
    – Jeff Schaller
    Mar 16 at 23:45







  • 2




    Looks like it's from github.com/aelsabbahy/goss/commit/… ? I don't see any obvious reason to do that. Maybe ask the author?
    – Mikel
    Mar 16 at 23:47










  • A possible reason: How to read the whole shell script before executing it?
    – Stéphane Chazelas
    Mar 17 at 8:53












  • 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 at 23:31










  • It might be you're looking for unix.stackexchange.com/q/122845/135943, but it's hard to tell.
    – Wildcard
    Mar 16 at 23:32










  • Or possibly unix.stackexchange.com/a/306141/117549 or unix.stackexchange.com/q/164482/117549 or unix.stackexchange.com/a/148480/117549
    – Jeff Schaller
    Mar 16 at 23:45







  • 2




    Looks like it's from github.com/aelsabbahy/goss/commit/… ? I don't see any obvious reason to do that. Maybe ask the author?
    – Mikel
    Mar 16 at 23:47










  • A possible reason: How to read the whole shell script before executing it?
    – Stéphane Chazelas
    Mar 17 at 8:53







1




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 at 23:31




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 at 23:31












It might be you're looking for unix.stackexchange.com/q/122845/135943, but it's hard to tell.
– Wildcard
Mar 16 at 23:32




It might be you're looking for unix.stackexchange.com/q/122845/135943, but it's hard to tell.
– Wildcard
Mar 16 at 23:32












Or possibly unix.stackexchange.com/a/306141/117549 or unix.stackexchange.com/q/164482/117549 or unix.stackexchange.com/a/148480/117549
– Jeff Schaller
Mar 16 at 23:45





Or possibly unix.stackexchange.com/a/306141/117549 or unix.stackexchange.com/q/164482/117549 or unix.stackexchange.com/a/148480/117549
– Jeff Schaller
Mar 16 at 23:45





2




2




Looks like it's from github.com/aelsabbahy/goss/commit/… ? I don't see any obvious reason to do that. Maybe ask the author?
– Mikel
Mar 16 at 23:47




Looks like it's from github.com/aelsabbahy/goss/commit/… ? I don't see any obvious reason to do that. Maybe ask the author?
– Mikel
Mar 16 at 23:47












A possible reason: How to read the whole shell script before executing it?
– Stéphane Chazelas
Mar 17 at 8:53




A possible reason: How to read the whole shell script before executing it?
– Stéphane Chazelas
Mar 17 at 8:53










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










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 case, 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.






share|improve this answer
















  • 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 at 8:33







  • 2




    See How to read the whole shell script before executing it?
    – Stéphane Chazelas
    Mar 17 at 8:36










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430699%2fwhy-enclose-a-shell-script-in-curly-braces%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










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 case, 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.






share|improve this answer
















  • 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 at 8:33







  • 2




    See How to read the whole shell script before executing it?
    – Stéphane Chazelas
    Mar 17 at 8:36














up vote
3
down vote



accepted










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 case, 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.






share|improve this answer
















  • 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 at 8:33







  • 2




    See How to read the whole shell script before executing it?
    – Stéphane Chazelas
    Mar 17 at 8:36












up vote
3
down vote



accepted







up vote
3
down vote



accepted






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 case, 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.






share|improve this answer












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 case, 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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 17 at 8:24









Kusalananda

103k13201317




103k13201317







  • 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 at 8:33







  • 2




    See How to read the whole shell script before executing it?
    – Stéphane Chazelas
    Mar 17 at 8:36












  • 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 at 8:33







  • 2




    See How to read the whole shell script before executing it?
    – Stéphane Chazelas
    Mar 17 at 8:36







2




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 at 8:33





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 at 8:33





2




2




See How to read the whole shell script before executing it?
– Stéphane Chazelas
Mar 17 at 8:36




See How to read the whole shell script before executing it?
– Stéphane Chazelas
Mar 17 at 8:36












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f430699%2fwhy-enclose-a-shell-script-in-curly-braces%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos