Learning the bash Shell: Unix Shell Programming (In a Nut... and over 2 million other books are available for Amazon Kindle . Learn more
  • RRP: £23.50
  • You Save: £4.53 (19%)
FREE Delivery in the UK.
Only 6 left in stock (more on the way).
Dispatched from and sold by Amazon.
Gift-wrap available.
Learning the bash Shell: ... has been added to your Basket
Condition: Used: Good
Trade in your item
Get a £4.25
Gift Card.
Have one to sell?
Flip to back Flip to front
Listen Playing... Paused   You're listening to a sample of the Audible audio edition.
Learn more
See all 2 images

Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly)) Paperback – 8 Apr 2005

See all 6 formats and editions Hide other formats and editions
Amazon Price New from Used from
Kindle Edition
"Please retry"
"Please retry"
£11.91 £11.69

Frequently Bought Together

Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly)) + bash Cookbook: Solutions and Examples for bash Users (Cookbooks (O'Reilly)) + Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix
Price For All Three: £66.84

Buy the selected items together

Trade In this Item for up to £4.25
Trade in Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly)) for an Amazon Gift Card of up to £4.25, which you can then spend on millions of items across the site. Trade-in values may vary (terms apply). Learn more

Product details

  • Paperback: 354 pages
  • Publisher: O'Reilly Media; 3 edition (8 April 2005)
  • Language: English
  • ISBN-10: 0596009658
  • ISBN-13: 978-0596009656
  • Product Dimensions: 17.8 x 2.3 x 23.3 cm
  • Average Customer Review: 3.0 out of 5 stars  See all reviews (2 customer reviews)
  • Amazon Bestsellers Rank: 352,854 in Books (See Top 100 in Books)
  • See Complete Table of Contents

Product Description

About the Author

Cameron Newham lives in Perth, Western Australia. After completing a Bachelor of Science majoring in information technology and geography at the University of Western Australia, Cameron joined Universal Defence Systems (later to become Australian Defence Industries) as a software engineer. He has been with ADI for six years, working on various aspects of command and control systems. In his spare time Cameron can be found surfing the Internet, ballroom dancing, or driving his sports car. He also has more than a passing interest in space science, 3D graphics, synthesiser music, and Depeche Mode.

Excerpt. © Reprinted by permission. All rights reserved.

CHAPTER 4 Basic Shell Programming

If you have become familiar with the customization techniques we presented in the previous chapter, you have probably run into various modifications to your environment that you want to make but can’t—yet. Shell programming makes these possible.

bash has some of the most advanced programming capabilities of any command interpreter of its type. Although its syntax is nowhere near as elegant or consistent as that of most conventional programming languages, its power and flexibility are comparable. In fact, bash can be used as a complete environment for writing software prototypes.

Some aspects of bash programming are really extensions of the customization techniques we have already seen, while others resemble traditional programming language features. We have structured this chapter so that if you aren’t a programmer, you can read this chapter and do quite a bit more than you could with the information in the previous chapter. Experience with a conventional programming language like Pascal or C is helpful (though not strictly necessary) for subsequent chapters. Throughout the rest of the book, we will encounter occasional programming problems, called tasks, whose solutions make use of the concepts we cover.

Shell Scripts and Functions

A script (a file that contains shell commands) is a shell program. Your .bash_profile and environment files, discussed in the previous chapter, are shell scripts.

You can create a script using the text editor of your choice. Once you have created one, there are two ways to run it. One, which we have already covered, is to type source scriptname. This causes the commands in the script to be read and run as if you typed them in.

The second way to run a script is simply to type its name and hit RETURN, just as if you were invoking a built-in command. This, of course, is the more convenient way. This method makes the script look just like any other UNIX command, and in fact several "regular" commands are implemented as shell scripts (i.e., not as programs originally written in C or some other language), including spell, man on some systems, and various commands for system administrators. The resulting lack of distinction between "user command files" and "built-in commands" is one factor in UNIX’s extensibility and, hence, its favored status among programmers.

You can run a script by typing its name only if the directory where the script is located is in your command search path, or . (the current directory) is part of your command search path, i.e., the script’s directory path (as discussed in Chapter 3). If these aren’t in your path, you must type ./scriptname, which is really the same thing as typing the script’s absolute pathname (see Chapter 1).

Before you can invoke the shell script by name, you must also give it "execute" permission. If you are familiar with the UNIX filesystem, you know that files have three types of permissions (read, write, and execute) and that those permissions apply to three categories of user (the file’s owner, a group of users, and everyone else). Normally, when you create a file with a text editor, the file is set up with read and write permission for you and read-only permission for everyone else.

Therefore you must give your script execute permission explicitly, by using the chmod command. The simplest way to do this is to type:

$ chmod +x scriptname

Your text editor will preserve this permission if you make subsequent changes to your script. If you don’t add execute permission to the script and you try to invoke it, the shell will print the message:

scriptname: Permission denied

But there is a more important difference between the two ways of running shell scripts. While using source causes the commands in the script to be run as if they were part of your login session, the "just the name" method causes the shell to do a series of things. First, it runs another copy of the shell as a subprocess; this is called a subshell. The subshell then takes commands from the script, runs them, and terminates, handing control back to the parent shell.

Figure 4-1 shows how the shell executes scripts. Assume you have a simple shell script called alice that contains the commands hatter and gryphon. In .a, typing source alice causes the two commands to run in the same shell, just as if you had typed them in by hand. .b shows what happens when you type just alice: the commands run in the subshell while the parent shell waits for the subshell to finish.

You may find it interesting to compare this with the situation in .c, which shows what happens when you type alice &. As you will recall from Chapter 1, the & makes the command run in the background, which is really just another term for "subprocess." It turns out that the only significant difference between .c and .b is that you have control of your terminal or workstation while the command runs — you need not wait until it finishes before you can enter further commands.

There are many ramifications to using subshells. An important one is that the exported environment variables that we saw in the last chapter (e.g., TERM, EDITOR, PWD) are known in subshells, whereas other shell variables (such as any that you define in your .bash_profile without an export statement) are not.

Other issues involving subshells are too complex to go into now; see Chapter 7 and Chapter 8 for more details about subshell I/O and process characteristics, respectively. For now, just bear in mind that a script normally runs in a subshell.

Inside This Book (Learn More)
Explore More
Browse Sample Pages
Front Cover | Copyright | Table of Contents | Excerpt | Index | Back Cover
Search inside this book:

Customer Reviews

3.0 out of 5 stars
5 star
4 star
3 star
2 star
1 star
See both customer reviews
Share your thoughts with other customers

Most Helpful Customer Reviews

2 of 3 people found the following review helpful By Daniela trogolo on 25 Dec. 2012
Format: Paperback Verified Purchase
I used this book to learn Bash language! It was very easy to read,but it's not for advanced student (also the title says this...). I appreciated it.
Comment Was this review helpful to you? Yes No Sending feedback...
Thank you for your feedback. If this review is inappropriate, please let us know.
Sorry, we failed to record your vote. Please try again
0 of 1 people found the following review helpful By Jascha Casadio on 24 Jan. 2015
Format: Paperback
I've told myself to get a book about bash so many times in the past that my Goodread`s Want to Read shelf was getting boringly monothematic. Last month I planned to get my hands on bash Cookbook but a comment on Amazon convinced me to dedicate my time to this title instead. To make it short, I'm not exactly enthusiast: some (just some!) parts were interesting; others (most!) were overly detailed and accompanied with complicated examples, a pain to get through.

This is a book that clearly targets beginners, people with close to no experience with Linux and the bash shell. If you work on a daily basis with the penguin, you better move along.

Ok so, let's imagine I recently moved from Windows to Linux and I want to explore what the bash shell offers me. What do I get off these 300 pages? Well, the book is divided in 3 parts:

Very basic shell features.
Basic shell scripting.
Basic shell features.

The first part, which covers the first three chapters, tells you about basic commands, such as "ls" and all the arguments it swallows. Unless you have never opened the terminal before, you might want to skip these pages.

Next the authors introduce some basic shell scripting, starting from variable naming to arrays and flow control. This was, by far, the most interesting part of the whole book in my opinion, but still, the author has covered only the very basics. What I've found particularly annoying was the choice to list all the possible options available just to find out, later, that the book wasn't about system programming so that they would have not been explained.

Finally, we leave the magic world of scripting and get introduced to other basic features, such as jobs: background foreground, handling signals.
Read more ›
Comment Was this review helpful to you? Yes No Sending feedback...
Thank you for your feedback. If this review is inappropriate, please let us know.
Sorry, we failed to record your vote. Please try again

Most Helpful Customer Reviews on Amazon.com (beta)

Amazon.com: 38 reviews
39 of 41 people found the following review helpful
Strong, gently-paced intro 24 Oct. 2006
By wiredweird - Published on Amazon.com
Format: Paperback Verified Purchase
The bash shell is now the most common and featureful command shell in the Unix world. It's full capability certainly isn't obvious to a beginner facing a command prompt, but is well worth exploring. This book is a great place for the novice to start. The first chapter addresses the most fundamental question: just what is a command shell?

The ideal reader already knows at least the names of the emacs and vi editors. That much helps understand the many features and two distinct feature sets available for command line editing. I consider fancy command line editing over-rated for fluent typists, but it's there in the second chapter for all who want it and anyone can benefit from at least a little knowledge of it. After that successive chapters pull the reader deeper into the bash feature set: aliases and shell variables, scripting and shell programming, and debugging when the shell programs or functions go awry.

Since this book is aimed at the novice, Newham and Rosenblatt skip lightly over a few of the more advanced subjects. For example, exceptions and trap handling get only cursory treatment, since they get into deep weirdness very fast. The authors are honest about this shallow treatment, though, and give enough information for a novice to recognize the basics and look them up in more advanced references.

This is nicely organized for the self-taught student. As a result, it's not laid out as a programmer's reference manual - anyone who wants that kind of reference just isn't looking at the right book. For its intended reader, though, it's a great book. It gets readers off to a fast start, and lets them decide just how much they want to bite off at a time. I recommned it very highly.

13 of 15 people found the following review helpful
Confusing 11 Feb. 2012
By Ian Belcher - Published on Amazon.com
Format: Paperback
I have been using UNIX for a few years, nothing too in depth, but I can get around. I recently went through the book Learning the UNIX Operating System, Fifth Edition without any trouble and then moved onto the book in question. Starting with the third chapter I felt completely lost and had no idea what the heck the author was talking about. The examples are vague and unhelpful. I kept reading, however, and into the fourth chapter the confusion persisted. Perhaps if the author decided to include some hands-on examples and/or exercises I might understand the concepts better.

I could see this being a worthwhile book if you know how to program already, but if you are just familiar with UNIX navigation, commands, (ie. anything in the Learning the UNIX Operating System, Fifth Edition book) then you might be out of luck.
9 of 10 people found the following review helpful
While anyone will learn some new things, the entire approach of the 3rd edition was obsolete by the time of its 2005 publication 4 Jan. 2014
By Christopher Culver - Published on Amazon.com
Format: Paperback
Cameron Newham's LEARNING THE BASH SHELL is an introduction to the command-line interface most commonly encountered today in server administration and in the terminal application of personal computers running Linux and Mac OS X. As I write this, the most recent edition is the 3rd, published in 2005, which describes bash 3.0. Newham explains such things as how programs communicate with the shell, keyboard navigation commands and shell customization. While he uses some of the old standard Unix programs (e.g. grep, sort) in examples, this is not a book about how to wield the power of Unix-like systems in general. Also, shell scripting is given only a brief mention, and those wanting to write powerful scripts will have to turn to another book (like O'Reilly's Classic Shell Scripting.

I have been using bash for nearly all file management and system administration tasks since 2002, and I still learned a few things here. However, this book is sorely in need of a new edition. The 3rd edition still assumes that the typical newcomer to bash is on a multi-user UNIX system, has access to a Postscript printer from the command line and a magnetic tape drive, and has probably used another shell like tcsh. Surely, even by the 3rd edition's publication date of 2005, most people interested in bash were people who had installed Linux on their personal computers. Also, bash is now at version 4.0, and readers would benefit from a small presentation of what has changed.
2 of 2 people found the following review helpful
Changed the way I look at my shell 22 Jan. 2014
By Adam J Miller - Published on Amazon.com
Format: Kindle Edition Verified Purchase
This book earnestly changed the way I look at my shell, I recommend it to anyone who's not a bash shell veteran. This book is very well written and is paced in such a way that the vast topic of the bash shell can be consumed, understood, and put to use. Highly recommended.
2 of 2 people found the following review helpful
A good reference 14 Dec. 2013
By Amazon Customer - Published on Amazon.com
Format: Kindle Edition Verified Purchase
I work in so many languages I am ways forgetting syntax and BASH's syntax is a bit unusual. So this book is a reference text for me.
Were these reviews helpful? Let us know