Solved

Bash: code block in round braces

Posted on 2011-02-22
10
1,020 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

792 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