Solved

Bash: code block in round braces

Posted on 2011-02-22
10
1,004 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 11

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
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)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

22 Experts available now in Live!

Get 1:1 Help Now