Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Bash Quick Start Guide

You're reading from   Bash Quick Start Guide Get up and running with shell scripting with Bash

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher Packt
ISBN-13 9781789538830
Length 186 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Tom Ryder Tom Ryder
Author Profile Icon Tom Ryder
Tom Ryder
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. What is Bash? FREE CHAPTER 2. Bash Command Structure 3. Essential Commands 4. Input, Output, and Redirection 5. Variables and Patterns 6. Loops and Conditionals 7. Scripts, Functions, and Aliases 8. Best Practices 9. Other Books You May Enjoy

Understanding Bash features

Some of Bash's programming features are shared by all other Bourne-style, POSIX-compatible Shell scripting languages. These are specified by the POSIX standard, in section 2, Shell Command Language. Bash is designed to conform to this standard. You can read what it specifies at http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html.

Bash also has many other features that are not described in the POSIX shell script standard that make many common shell script programming tasks easier. In this book, we will examine both categories.

POSIX shell script features

Features required for the POSIX shell script include:

  • Running commands convenient syntax for running commands, including other programs, specifying arguments, environment variables, working directories, permissions masking, and other properties.
  • Variables that can be set to any string value, including manipulating process environment variables, and that can be expanded on the command line.
  • Arithmetic expansion for performing integer-based arithmetic on variables and numbers.
  • Control structures for executing code depending on the outcome of another command (if), including a specially-designed test or [ command, and repeating code until a condition is true (while).
  • Aliases as a way to abbreviate long command lines as a single word for convenience.
  • Functions to allow defining blocks of code as new commands for the purposes of the running script or interactive session.
  • Input and output redirection to specify input sources and output destinations for programs run in the shell.
  • Pipes to direct the output of one command to become the input of another.
  • Argument list through which code can iterate using a for loop.
  • Parameter expansion a means of switching between or transforming string values in assignments or command arguments.
  • Pattern matching in the form of classic Unix globs.
  • Process management in running jobs in the background, and waiting for them to complete at the desired point.
  • Compound commands to treat a group of commands as one, optionally running it in a subshell environment (subprocess).
  • Reading lines of input data including breaking them down into fields with a defined separator character.
  • Formatted strings such as the C printf(3) function in the stdio C programming language library.
  • Command substitution to use the output of a command as a variable, as part of a test, or as an argument to another command.

These features are either part of the Shell scripting language itself, or available in the POSIX-specified programs that it calls. In a sense, your shell scripts are only limited by the programs you can run with them.

By calling the grep program, for example, you can select input lines using regular expressions, even if your Shell scripting language does not itself support regular expressions. We will cover some of these essential commands in this book, even though they are not technically part of the GNU Bash distribution.

All the these features mean you can get a lot done in your Bash program even if you just use POSIX features, and your script might then run on other shells, such as dash, without much modification. All of the features are discussed in this book.

Bash-specific features

In addition to all the POSIX shell script features in the previous section, Bash adds many extensions that make programming more convenient, expressive, and sometimes less error-prone. These include:

  • Named array variables. This is perhaps the most important advantage over the plain POSIX shell script. It makes many otherwise impractical things possible. If you need one single reason to use Bash, this is probably it!
  • An easier syntax for performing conditional tests. This is also a very important feature.
  • Extended globs for advanced pattern-matching.
  • Regular expression support, for performing the most powerful kind of text-pattern-matching, when even globs won't do.
  • Local variables for functions, a limited kind of variable scope.
  • A C-style for loop syntax.
  • Several kinds of parameter expansion, including case transformation, array slices, substrings, substitution, and quoting.
  • Arithmetic expressions, for conveniently testing the outcome of arithmetic operations.
  • Many more shell options to control shell script and interactive shell behavior, including extra debugging support.
  • Better support for irregular filenames and unusual line separators in data.

All of these features are also discussed in this book. Where relevant, we will specify which features are POSIX-specific and which features are specific to Bash.

Do I need Bash?

If you know you will have Bash available on the systems where your shell script will run, you should use it! Its features make programming in shell script easier and safer, and it is by far the most popular kind of shell script, with many people reading and writing it. Many people think that Bash is the only kind of shell script.

Even if your shell script is simple today, you might need to add more to it tomorrow, and a Bash feature might be exactly the thing you need at that time.

However, if your shell script might need to run on a system where Bash may not be installed and cannot be installed for your script, then you may need to limit yourself to the POSIX shell features. Check your system's documentation to determine what style of shell script you will have to write in order for your script to run.

You have been reading a chapter from
Bash Quick Start Guide
Published in: Sep 2018
Publisher: Packt
ISBN-13: 9781789538830
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime