0

Can some one please explain the below statements?

$ x=50

$ (x=100) #here the code executes only with in the subshell. when subshellexecution done then the x value is returned to its original value, i.e. 50

$ echo $x

$ 50


$ x=50

$ { x=100; } #here the x value totaly changes and affecting to the current shell value of x and changes it from 50 to 100

$ echo $x

$ 100


how these are happening, Can some one explain?

3

From man bash:

(list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes.

A command invoked in the separate environment cannot affect the shell's execution environment.

{ list; } list is simply executed in the current shell environment.

3

In Bourne-like shells, ( ... ) executes the code in a subshell, thus any change in the variables is local to this subshell, and not visible in the parent shell. On the contrary { ... } executes code in the current shell, thus affecting its variables.

1

As you said: ( ) causes a subshell. { ;} doesn't. That's why the results differ. { ;} doesn't make sense for single commands anyway. It's a grouping feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.