Think of it like this: Parentheses create a temporary "bubble" (clone), while Curly Braces are just a way to group instructions in the room you are already standing in.
1. Parentheses ( ) - The Subshell
When you use ( ), the shell creates a child process (a subshell).
Isolation: The subshell gets a copy of everything from the parent, but it cannot pass anything back.
The "Vegas Rule": What happens in the subshell, stays in the subshell. If you change a variable inside
( ), it vanishes the moment the closing)is reached.
Example:
name="Mahesh"
(
name="Ahana"
echo "Inside parens: $name"
)
echo "Outside parens: $name"
Output:
Inside parens: Ahana
Outside parens: Mahesh <-- The original variable was protected!
2. Curly Braces { } - The Current Shell
When you use { }, you are simply grouping commands together in the current process.
Integration: Everything happens in the "here and now."
Permanent Changes: If you change a variable inside
{ }, it is changed for the rest of the script.Syntax Note: Braces require a
;(or newline) before the closing}and spaces after the opening{.
Example:
name="Mahesh"
{
name="Ahana"
echo "Inside braces: $name"
}
echo "Outside braces: $name"
Output:
Inside braces: Ahana
Outside braces: Ahana <-- The original variable was overwritten!
No comments:
Post a Comment