Bash what?

Introduction

Preliminaries

Simple Stuff

Where are you?

Listing Files

Examining Files

There are many, many more shell commands to learn and to use. You may want to browse the list of for more detail.

Pipelines and Redirection

Shell Script Basics

First Shell Script

Tests and Branching

Loops and Repetition

Using Numbers in Scripts

Coping with user input

An advanced example with numbers and user input

Creating and using arrays

Strings and substrings

Searching and Replacing Substrings within Strings

More obscure but useful string operations

Bash Version 3

I have always thought the inability to test for the presence of a string or pattern (without using grep, sed or something similar) was a conspicuous weakness in shell programming. Bash version 3, present on must current Linux distributions, addresses this lack by allowing regular expression matching.

Let's say we need to establish whether variable $x appears to be a social security number:

if [[ $x =~ [0-9]{3}-[0-9]{2}-[0-9]{4} ]] then # process SSN else # print error message fi

Notice the Perlish "=~" syntax and that the regular expression appears within double brackets. A substantial number of regular expression metacharacters are supported, but not some of the Perl-specific extensions like \w, \d and \s.

Another Bash 3 feature is an improved brace expansion operator:

$ echo {a..z} a b c d e f g h i j k l m n o p q r s t u v w x y z