The newline Guide to Bash Scripting
Bash is the most popular shell and here we'll teach you popular tools, techniques, pitfalls and workarounds. Bash is simple to get started with, hard to master, and you can do incredible things with a line or two – inside we'll teach you how.
What You Will Learn
How to find help on arbitrary commands
How to create robust, maintainable scripts
The importance of error handling settings
How to use many popular tools
What Bash is good and bad at
Bash is everywhere. It has grown to be the most popular shell on *nixes – including Mac OS X and Windows Subsystem Linux (WSL).
The newline Guide to Bash Scripting is an exploration of the Bash shell, scripting language and related tools. It’s aimed at developers who want to get the job done right the first time, and make sure maintenance is a breeze. Topics include:
- how to find help on arbitrary commands
- how to structure a robust and maintainable script
- editing commands and interacting with the terminal efficiently
- tools for dealing with version control, JSON, XML, text encodings, images and compressed files
- quality assurance
- … and much more
The goal of this book is to give you the tools and techniques to write Bash scripts which will stand the test of time, and to give you the means to explore the vast shell scripting ecosystem to find the tools you need right now.
Audience
Bash is the Linux glue language, so this book is aimed at software developers mainly working in some other language. We assume that you have working knowledge of programming concepts like variable assignments, loops and files, and at least a passing familiarity with interactive shells.
If you're comfortable with web development, but not–so–comfortable on the commandline then this course is perfect for you.
Course Content
Introduction
LESSON
Community and Code Downloads
Here's where you'll find the code downloads and Discord channel
LESSON
Conventions used in this book
First stop, orientation
LESSON
LESSON
LESSON
LESSON
Self–Help
Command documentation
Precedence is the same here as you might be familiar with from arithmetic. For example, multiplication has higher precedence than addition, so 2 + 3 × 4 is equal to 2 + (3 × 4), or 14. A common use case for this is to define a function with the same name as a file command to set some default options. As a quick example, here’s how we would tell `grep` to use colored output by default:
LESSON
LESSON
LESSON
LESSON
Running Scripts
Copying and Pasting
LESSON
LESSON
LESSON
LESSON
LESSON
Listing Files
Math
LESSON
LESSON
Arithmetic evaluation
then echo "x is bigger" else echo "y is bigger" fi y is bigger \```
LESSON
LESSON
LESSON
LESSON
Time
JSON
XML
LESSON
Pulling out a value
<configuration> <password>foo & bar</password> </configuration> EOF $ password="$(xml_grep --text_only '/configuration/password' ./test.xml)" $ printf '%s\n' "$password" foo & bar ```
LESSON
LESSON
Creating
\--stringparam username jdoe --stringparam password 'foo > bar' ./authentication.xslt - <<< '<x/>' \```
LESSON
LESSON
Images
LESSON
LESSON
LESSON
LESSON
Metadata
\--tag=Copyright ./image.jpg Wrote file './new.jpg'. $ exif --tag=Copyright --machine-readable ./new.jpg Copyright Victor Engmark (Photographer) - [None](Editor) \```
LESSON
Archiving and Compression
SSH
LESSON
LESSON
LESSON
LESSON
From the Terminal to Production
LESSON
LESSON
LESSON
LESSON
LESSON
Including files
' \```
LESSON
Cleaning up
example text $ echo "$line" original value \```
LESSON
Fail–Fast Settings
Version Control
Quality Assurance
Signals
Autocompletion
Your Instructors
Frequently Asked Questions
Who is this course for?
This book is aimed at software developers mainly working in some other language who want to get better at using the CLI and writing Bash Scripts.
What if I need help?
You can ask us questions anytime through the community Discord channel.