Wednesday, November 13, 2013

Linux - Back To Basic Part 4 - Grouping Commands (...) and {...}

Discovered more things when reading on Bash reference manual

This time round, it is about Grouping Commands

There are 2 grouping command and they are (...) and {...}. Now, don't get confused with ${...} and $(...), they are parameter substitution and command substitution respectively.

For the following commands

(ls -ls; echo "hello world")

{ ls -ls; echo "hello world"; }

Both of them group the commands and execute as a unit.

But the differences is a following

(ls -ls; echo "hello world") will create a subshell and execute each command in the subshell. Since (...) execute commands in a subshell, any environment changes, such as setting environment variable, will not be retained and passed back to the parent shell

{ ls -ls; echo "hello world"; } will execute each command in the current shell. Thus, any environment changes will be retained. Please note that when using { and } are reserved word and a meta-characters such as meta-characters (`<', `>', `|', `;', `(', `)', and `&'). As such, there must a space between the start of the braces and the first command. Also, a semicolon is required after each command.

No comments:

Post a Comment

Hadoop - How to setup a Hadoop Cluster

Below is a step-by-step guide which I had used to setup a Hadoop Cluster Scenario 3 VMs involved: 1) NameNode, ResourceManager - Host...