Solved

Bash: code block in round braces

Posted on 2011-02-22
10
1,030 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

749 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