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

PuTTY - Keeping session alive

If you have some VPN tunnels that timeout based on activity, you could try the following setting if you are using PuTTY 1) Open PuTTY and...