LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-10-2009, 05:01 PM   #1
krytron
LQ Newbie
 
Registered: Oct 2009
Distribution: Ubuntu 9.04
Posts: 4

Rep: Reputation: 0
Learning compound commands


Hello, this is my first time posting.

I'm learning compound commands. The article I'm reading gave some simple examples on how to use them, for instance:

(cd Documents; ls); ls

but I can accomplish the same thing by typing:

ls Documents; ls

There are other commands I've tried and all of them have an alternative to using parentheses and require less typing. So I'm wondering, if using compound commands is only useful for more advanced commands or shell scripts? I haven't tried it yet but I guess if you were to install updates in the shell you could go:

(sudo apt-get install updates)

That way it would run in the sub shell and you could continue working in the parent shell.
 
Old 10-10-2009, 05:05 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Welcome to LQ!!

I'm not really clear what your question is but it think you can see how this works by just trying it.

Your two examples are NOT the same---in the first case, you will be left in "Documents"--in the second case, you'll stay in the parent directory.
 
Old 10-10-2009, 05:28 PM   #3
krytron
LQ Newbie
 
Registered: Oct 2009
Distribution: Ubuntu 9.04
Posts: 4

Original Poster
Rep: Reputation: 0
Hi pixellany, thank you for the welcome

Well the commands are different but have the same outcome. After I type in both examples I remain in my home directory. In both cases my prompt doesn't change and I also checked by typing "pwd" after both examples and both times I remained in my home directory. In the first example, the first part of the command in executed in the subshell and doesn't affect the working directory.

I get the same outcome in both examples, only the latter requires less typing.
So that's what I'm wondering, what is using parentheses good for? More advanced stuff like shell scripting?
 
Old 10-10-2009, 05:43 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
The differences become more obvious once you start using shell variables, or modifying them.
The first version would be executed in a sub-shell, and leave the calling shells variables
intact. The second would 'permanently' change them. The visible result in your example is
the same. The 'hidden results' are different. In your first example $PWD would have had
different values during the execution.


Cheers,
Tink
 
Old 10-10-2009, 06:17 PM   #5
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 128Reputation: 128
{ } and ( ) are used to isolate a group of commands from the parent script, they will have no impact on any variables as explained earlier. You can also pipe the output of the { } or ( ) bloc as a single command.

Ex:

Code:
{ echo apples
echo bananas
echo oranges } | grep an
will print:
bananas
oranges

you can also use it to hide some unwanted printing:
Code:
echo strawberries
{ echo apples
echo bananas
echo oranges } > /dev/null
echo grapes
will print only strawberries and grapes, evn if each command were executed.

I hope it make a little more obvious what brace and parenthesis do in bash.
 
Old 10-10-2009, 06:44 PM   #6
krytron
LQ Newbie
 
Registered: Oct 2009
Distribution: Ubuntu 9.04
Posts: 4

Original Poster
Rep: Reputation: 0
Tinkster-I sort of understand what you are saying but I lack the understanding of technical terms such as shell variables.

Elv13-I understand your echo examples but I'm still confused.

Would you guys be willing to recommend some articles, tutorials, books free or not, that will give me a solid understanding of the bash shell. I've read a bit of both rute and intro to linux from tldp.org but seem to get overwhelmed with them. But if those two books are the best for beginners I will read them.

Thanks for the help!
 
Old 10-10-2009, 07:07 PM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by krytron View Post
Tinkster-I sort of understand what you are saying but I lack the understanding of technical terms such as shell variables.

Elv13-I understand your echo examples but I'm still confused.

Would you guys be willing to recommend some articles, tutorials, books free or not, that will give me a solid understanding of the bash shell. I've read a bit of both rute and intro to linux from tldp.org but seem to get overwhelmed with them. But if those two books are the best for beginners I will read them.

Thanks for the help!
There are plenty of excellent Bash/shell tutorials. Here's mine:

Bash Shell Programming in Linux

I emphasize there are many others just as good or better.
 
Old 10-10-2009, 07:20 PM   #8
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 128Reputation: 128
This is the best guide I know of:
http://tldp.org/LDP/abs/html/index.html
 
Old 10-10-2009, 07:41 PM   #9
mrrangerman
Member
 
Registered: Oct 2007
Location: MI
Distribution: Debian Slackware
Posts: 528

Rep: Reputation: 50
Here is a good start Rute

You can also use the {} braces for shell/brace expansion. Say you want to make several directories with a common name and postfix them with numbers. You can do mkdir name1 then mkdir name2 and so on or you can take advantage of shell expansion and use the command
Code:
mkdir name{1..9}
and this will create all the directories from name1 through name9 with one command. You can prefix in the same way
Code:
mkdir {1..9}name
.
This will also work in making multiple files, with the touch command
Code:
touch file{1..9}.txt

Last edited by mrrangerman; 10-11-2009 at 01:28 AM. Reason: fix spelling
 
Old 10-10-2009, 11:30 PM   #10
cola
Senior Member
 
Registered: Sep 2007
Location: Dhaka,Bangladesh
Distribution: Debian
Posts: 1,031

Rep: Reputation: 63
http://ss64.com/bash/
 
Old 10-11-2009, 02:05 AM   #11
krytron
LQ Newbie
 
Registered: Oct 2009
Distribution: Ubuntu 9.04
Posts: 4

Original Poster
Rep: Reputation: 0
Thank you everyone for your help and suggestions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning shell commands edenracer Linux - Newbie 5 10-02-2009 04:52 AM
Learning commands for CLI in Ubuntu Gutsy zambonibutt Linux - Newbie 2 10-26-2007 12:53 PM
LXer: Learning About Linux Commands LXer Syndicated Linux News 0 08-25-2006 05:54 PM
Quick question: Learning commands Kami.JZ Linux - Newbie 4 10-24-2004 05:14 PM
learning in-depth terminal commands hildog Linux - Newbie 2 09-30-2003 02:28 PM


All times are GMT -5. The time now is 01:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration