If your daily job was to run certain commands at a particular time without fail, you would definitely get bored over time and chances of forgetting to execute the commands at the specified time are high. What if you just write the set of instructions of what you are required to execute and instruct Bash to execute during the required time? That would be cool, right? This is where Bash scripting comes in handy.
Bash (Bourne Again Shell) is a command line interpreter and is the default for most Linux systems. Shell is a processor enabling interactive and non-interactive command execution. A script explains what is to be done. Bash scripts are a series of commands that tell Bash what to do. Bash scripting allows automated command execution instead of running the commands one-by-one on the terminal.
Bash scripts are executable files containing all the commands you would wish to run at the same time. The files have .sh file extension. You can easily create a bash script using an editor such as vi or vim. For example, let’s create one example.sh, a bash script that backups from a certain folder and store on another. On your terminal create the file as below, I am using vim.
Add the following content:
#!/bin/bash #typically the first line of the bash scripts BACKUPTIME=`date +%b-%d-%y` #get the current date DESTINATION=/home/usr/path/backup-$BACKUPTIME.tar.gz #create a backup file using the current date in it's name SOURCEFOLDER=/home/usr/path/data_folder #the folder that contains the files that we want to backup tar -cpzf $DESTINATION $SOURCEFOLDER #create the backup
Save the file and make it executable by running the below command.
chmod +x example.sh
You can then schedule your script to be executing at specific times using cron job
$ crontab -e 29 0 * * * /bin/bash /path/example.sh
Here are some of the most interesting and free books that can help you learning Bash scripting within a short time.
1. Advanced Bash-Scripting Linux Guide – M Cooper (HTML)
The book is good for both newbies and advanced learners. For learners, the books presents the basics of bash scripting, introducing variables, parameters and special characters which are key in writing scripts with elaborate examples. The book then moves on to beyond basics and to advanced. It will highly benefit a beginner who is looking forward to advancing to productive scripting. Some of the notable topics are as below:
- Special characters.
- Introduction to variables and parameters
- manipulating variables
- Loops and branches
- Command substitution
- Regular expressions
- Network programming
2.Bash Guide for Beginners (2008) – M. Garrels (HTML)
As the title suggests, the book is meant for those who are getting their hands for the first time in Bash scripting. It first talks about common shell programs and outlines the advantages of using Bash. It also guides on how to develop good scripts and goes ahead to show script basics, how to create and run scripts as well as how to debug, all shown with examples. Among other useful scripting information to get from the book include:
- Regular expressions
- Conditional statements
- Writing interactive scripts
- Interactive editing with sed
- Introduction to gawk
- Repetitive tasks with for and while loop
3. Bash Notes for Professionals – Compiled from StackOverflow documentation (PDF)
The guide contains basic and advanced information for Bash users. For those who already know Linux terminal and are looking to get started on Bash scripting, book provides useful information, from introduction to Bash, common commands and functions to advanced Bash scripting.
4. BASH Programming (2000) – Mike G. (HTML)
The book is suitable for new learners. It touches on very important concepts of scripting, with clear and easy examples. These concepts include:
- Conditionals and loops
Link: BASH Programming (2000)
5. Bash Reference Manual (HTML)
The books begin from the very introduction of Shell and Bash, extends to basic shell features which covers operations such as quoting, commands, variables, parameters, functions and re-directions. The book also advances to other important scripting operations which include:
- Job control
- Using history interactively
- How to install Bash.
It is suitable for both beginners and advanced learners.
Link: Bash Reference Manual
6. Bash Tutorial – Anthony Scemama (PDF)
Though not so detailed, the ‘Bash Tutorial’ book provides useful information on how to use shell interactively. It is not suitable for beginners who are looking to first understand what Linux is and how to use the command line. It is, however, good for intermediate learners who already know about Linux and Bash and looking to get more on interactive shell. Major topics are:
- Interactive Bash
- Useful Linux commands
- Writing scripts
Link: Bash tutorial
7. Bash Guide – Maarten Billemont (HTML and PDF)
The guide intends to help people who want to learn to use Bash. It aims at teaching good practice techniques for using Bash and how to write simple scripts. It targets beginners assuming no advance knowledge to Bash scripting but are able to open a Linux terminal. The guides begins with the basic definition of Bash and proceeds to how to use it in scripting.
8. Conquering the Command Line – Mark Bates (HTML)
If you are new in using Linux command line and you wish to learn, this is the best book for you. It presents the most important command line utilities with examples of how to use them. The book basically gets one into understanding how to manipulate Linux files and directories from the command line. These include:
- Basics and navigation e.g listing files and directories, links, create, copy, move and delete files and directories.
- Ack/Ag commands.
- Sed and grep commands for editing and matching files
- ps for checking running processes
- Other important commands incude: tar, kill, tail, man
9. Getting Started with Bash (HTML)
This is a basic guide on what Bash is. It introduces Bash shell and Bash configurations files. It guides on how to modify bash shell with the set command. It also features some important Bash Commands such as:
- Searching Bash history
- Dealing with spaces; backslash escape sequence, single/double quotes with spaces and variables
- Executing multiple commands in sequence
- Pipelining output from one command to another
- Aliasing commands
- Altering command prompt look and information.
10. Google Shell Style Guide – Paul Armstrong (HTML)
Google Shell Style Guide introduces Shell, guiding on which shell to use and when to use. It then goes ahead to explain shell usage with examples. The topics are as below:
|Background||Which Shell to Use – When to use Shell|
|Shell Files and Interpreter Invocation||File Extensions – SUID/SGID|
|Environment||STDOUT vs STDERR|
|Comments||File Header – Function Comments – Implementation Comments – TODO Comments|
|Formatting||Indentation – Line Length and Long Strings – Pipelines – Loops – Case statement – Variable expansion – Quoting|
|Features and Bugs||ShellCheck – Command Substitution – Test, [… ], and [[… ]] – Testing Strings – Wildcard Expansion of Filenames – Eval – Arrays – Pipes to While – Arithmetic|
|Naming Conventions||Function Names – Variable Names – Constants and Environment Variable Names – Source Filenames – Read-only Variables – Use Local Variables – Function Location – main|
|Calling Commands||Checking Return Values – Builtin Commands vs. External Commands|
Link: Google Shell Style Guide
11. Introduction to the Command Line – Launch School (HTML)
Just like ‘Conquering the Command Line’ by Mark Bates, ‘Introduction to the Command Line’ book gives a friendly guide to getting familiar with using Linux command line from a beginner level to getting comfortable with the terminal. It’s core topics are:
- The command line interface
- Files, Directories and Executables
- The environment
12. Linux Shell Scripting Tutorial – A Beginner’s Handbook (2002) – Vivek G. Gite (HTML)
The book assumes that the learner does not have a prior knowledge about Linux and it begins by introducing what Linux and how to install it. It then steps to what shell and shell scripting is. Once the learner is familiar with Linux and shell, the book introduces shell programming, from basics to an advanced level. There is also a while chapter of shell scripting examples for the user to learn from.
13. Linux Shell Scripting Tutorial (LSST) v2.0 – Vivek Gite (HTML)
This is another important book for learners who are beginning from the basic level o Linux. The book introduces Linux; how to download and install, Linux kernel, Linux shell and how to use it and what shell scripts and shell scripting is all about. The major topics include:
- Quick introduction to Linux
- Getting Started with Shell Programming
- The Shell variables and Environment
- Conditional Execution
- Piping and Filters
- Interactive scripts.
14. Slackbook (2005) – Alan Hicks, Chris Lumens, David Cantrell and Logan Johnson (HTML, DocBook, Postscript, PDF).
Slackware Linux is an old Linux distribution developed by Patrick Volkerding in 1993. Slackbook is a comprehensive guide on getting started with Slackware Linux. The book is also called Slackware Linux Essentials and it explains from what Linux is and proceeds to installation, configuration and using Slackware Linux. The major topics include:
- Introduction to Slackware Linux
- System configuration
- Network configuration
- The shell
- Filesystem structure
- Handling files and directories
- Process control
- Basic network commands.
Link: Slackbook (2005)
15. The Bash Academy – Maarten Billemont (HTML)
The ‘Bash Guide’ is suitable for both beginners and advanced learners as it introduces both basic and advanced concepts of Bash Shell. It guides on the safest and most robust ways of writing powerful bash scripts and also how to make efficient and speedy interactive use of shell. Some major topics include:
- Introduction to Bash
- Commands and arguments
- Variables and examples
- Test and conditionals
- Loops and Functions
- Asynchronous commands
- Colors and terminal commands.
Link: The Bash Academy
16. The Linux Command Line – William E. Shotts, Jr. (PDF)
The book is designed new command line users. It presents basics of command line use and shell scripting and also chapters having many common programs used on command line. It covers the following:
- Introduction to command line
- Learning the Shell
- Configuration and Environment
- Common tasks and essential tools
- Writing Shell Scripts
Link: The Linux Command Line
17. Writing Shell Scripts – William E. Shotts, Jr. (HTML)
The guide dives directly into writing shell scrips. It therefore requires the learner to already have Linux command line knowledge and how to run basic commands. The topics include:
- Writing Our First Script and Getting It to Work
- Editing the Scripts We Already Have
- Here Scripts
- Command Substitution and Constants
- Shell Functions
- Some Real Work
- Flow Control – Part 1
- Stay Out of Trouble
- Keyboard Input and Arithmetic
- Flow Control – Part 2
- Positional Parameters
- Flow Control – Part 3
- Errors and Signals and Traps (Oh My!) – Part 1
- Errors and Signals and Traps (Oh My!) – Part 2
Link: Writing Shell Scripts
Bash Scripting is very crucial for every System Administrator. It makes it easy to run repetitive commands, minimizing time used and chances of making mistakes. The above books should provide you with the necessary information required to getting started with Bash scripting, some books from the very basics to advanced. I hope you find them informative.