Solved

Bash: code block in round braces

Posted on 2011-02-22
10
1,009 Views
Last Modified: 2013-12-21
Hi,

I was just looking at a bash script included with some software I downloaded, and I noticed that the code blocks in a couple of for-loops are written with round brackets '()' instead of curly brackets '{}'.

Is there a difference?

Thanks!
0
Comment
Question by:alpha-lemming
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 34957414
yes
0
 

Author Comment

by:alpha-lemming
ID: 34957418
Well, that's helpful...

What is exactly the difference?
0
 
LVL 84

Expert Comment

by:ozo
ID: 34957455
What is exactly the code?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 34957912
Do you mean something like
for i in 1 2 3
do
   (
    command
    command
   )
done

Open in new window


0
 
LVL 48

Expert Comment

by:Tintin
ID: 34957926
Basic difference between

()  and {}

is that commands run within the brackets are run in a sub-shell and commands run within braces are run in the current shell.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:alpha-lemming
ID: 34960439
Ok, thanks.
Why would I want to run commands in a sub-shell as opposed to the current one?
0
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 34965878
Generally when you want to use them as a list.

For example, instead of doing

date >log
who >>log
ps -ef >>log

Open in new window


you can group them and direct the output from all the commands just one

(date;who;ps -ef) >log

Open in new window


This will run the commands in a sub-shell.

You can still group commands and run them in the current shell using braces.

{ date;who;ps -ef; } >log

Open in new window


note the spaces and trailing semi-colon.

The main difference between the () and {} notations is that that the cd command and any variables set in a sub-shell disappear when it completes.
0
 
LVL 12

Expert Comment

by:tel2
ID: 34966019
So would {} be more efficient than () in general, Tintin, since a new process is not spawned with {}?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 34966072
Yes, there is a slight overhead in starting a sub-shell, but it's very minor unless you are doing it thousands of times in a loop.
0
 

Author Closing Comment

by:alpha-lemming
ID: 35130467
Nice, thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now