Linux - NewbieThis 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.
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.
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.
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?
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.
{ } 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:
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.
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:
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 12:28 AM.
Reason: fix spelling
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.