Bash: code block in round braces

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!
alpha-lemmingAsked:
Who is Participating?
 
TintinConnect With a Mentor Commented:
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
 
ozoCommented:
yes
0
 
alpha-lemmingAuthor Commented:
Well, that's helpful...

What is exactly the difference?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ozoCommented:
What is exactly the code?
0
 
TintinCommented:
Do you mean something like
for i in 1 2 3
do
   (
    command
    command
   )
done

Open in new window


0
 
TintinCommented:
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
 
alpha-lemmingAuthor Commented:
Ok, thanks.
Why would I want to run commands in a sub-shell as opposed to the current one?
0
 
tel2Commented:
So would {} be more efficient than () in general, Tintin, since a new process is not spawned with {}?
0
 
TintinCommented:
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
 
alpha-lemmingAuthor Commented:
Nice, thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.