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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-10-2009, 04:01 PM
|
#1
|
LQ Newbie
Registered: Oct 2009
Distribution: Ubuntu 9.04
Posts: 4
Rep:
|
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.
|
|
|
10-10-2009, 04:05 PM
|
#2
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
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.
|
|
|
10-10-2009, 04:28 PM
|
#3
|
LQ Newbie
Registered: Oct 2009
Distribution: Ubuntu 9.04
Posts: 4
Original Poster
Rep:
|
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?
|
|
|
10-10-2009, 04:43 PM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
10-10-2009, 05:17 PM
|
#5
|
Member
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825
Rep: 
|
{ } 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.
|
|
|
10-10-2009, 05:44 PM
|
#6
|
LQ Newbie
Registered: Oct 2009
Distribution: Ubuntu 9.04
Posts: 4
Original Poster
Rep:
|
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!
|
|
|
10-10-2009, 06:07 PM
|
#7
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep: 
|
Quote:
Originally Posted by krytron
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.
|
|
|
10-10-2009, 06:20 PM
|
#8
|
Member
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825
Rep: 
|
|
|
|
10-10-2009, 06:41 PM
|
#9
|
Member
Registered: Oct 2007
Location: MI
Distribution: Debian Slackware
Posts: 528
Rep:
|
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
and this will create all the directories from name1 through name9 with one command. You can prefix in the same way
.
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 12:28 AM.
Reason: fix spelling
|
|
|
10-10-2009, 10:30 PM
|
#10
|
Senior Member
Registered: Sep 2007
Posts: 1,047
Rep:
|
|
|
|
10-11-2009, 01:05 AM
|
#11
|
LQ Newbie
Registered: Oct 2009
Distribution: Ubuntu 9.04
Posts: 4
Original Poster
Rep:
|
Thank you everyone for your help and suggestions.
|
|
|
All times are GMT -5. The time now is 12:44 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|