There are two different ways of grouping commands in unix shell script.
i) ( ) : When we group commands inside ( ), the commands are executed in the subshell, instead of current shell.
ii) { } : When we group commands inside { }, the commands are executed in the current shell.
Let's understand it with the help of an example. In this example, we will try to change the working directory of a shell and verify whether it has been changed or not.
Open shell and type the following command. Assuming that shell's pwd before executing the below command is /
$ { cd /etc/mail ; pwd ; }
The output will be
/etc/mail
This cd command was run on the current shell. If you want to verify, execute pwd again and you will see the same output
$ pwd
/etc/mail
Now, we will execute the same set of commands using ( ). Again assuming that pwd is /etc/mail
$ ( cd .. ; pwd )
The output will be
/etc
This cd command was run on the subshell. If you want to verify, execute pwd and you will see the output
$ pwd
/etc/mail
We can see that pwd of the current shell has not changed.
Note: Always use ; before } if { and } appears on the same line. This is not needed in case of ( ).
For more info visit
No comments:
Post a Comment