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.

5.0 201 students started

Published |Updated

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

22 modules99 lessons

Introduction

7 lessons

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

Self–Help

7 lessons

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

Editing Commands and Scripts

2 lessons

LESSON

LESSON

Copying and Pasting

6 lessons

Listing Files

3 lessons

Text

3 lessons

LESSON

LESSON

LESSON

Math

7 lessons

LESSON

Arithmetic evaluation

then echo "x is bigger" else echo "y is bigger" fi y is bigger \```

LESSON

LESSON

Time

4 lessons

Output

4 lessons

LESSON

LESSON

LESSON

LESSON

JSON

2 lessons

LESSON

Filtering

{"username":"jdoe","password":"sec\\ret$"} EOF $ jq . ./credentials.json { "username": "jdoe", "password": "sec\\ret$" } \```

LESSON

XML

5 lessons

LESSON

Pulling out a value

<configuration> <password>foo &amp; bar</password> </configuration> EOF $ password="$(xml_grep --text_only '/configuration/password' ./test.xml)" $ printf '%s\n' "$password" foo & bar ```

LESSON

Creating

\--stringparam username jdoe --stringparam password 'foo > bar' ./authentication.xslt - &lt;&lt;&lt; '<x/>' \```

LESSON

Images

6 lessons

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

5 lessons

From the Terminal to Production

8 lessons

LESSON

Cleaning up

example text $ echo "$line" original value \```

LESSON

Fail–Fast Settings

6 lessons

LESSON

LESSON

LESSON

pipefail

grep: ./README.md: No such file or directory

LESSON

failglob

do grep foo "$file" done grep: ./\*: No such file or directory \```

LESSON

Version Control

4 lessons

Gitting stuff done

# Fullstack Bash Scripting notes & code

LESSON

LESSON

Quality Assurance

3 lessons

Signals

3 lessons

Summary and Changelog

2 lessons

LESSON

LESSON

Your Instructors

Profile image for l0b0

Victor Engmark

👋 My name is Victor Engmark. I have been a software developer since 2004, free and open source software contributor since 2007, Stack Overflow & Stack Exchange contributor since 2009, and a Bash user since 2010. I’ve spent thousands of hours reading about Bash, answering questions about Bash and, most importantly, writing Bash for use in production systems.

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.

OrGet this course and every newline Book and Guide with a newline Pro subscription for just $20/mo

$39

$49

  • Learn to use the CLI

  • Automate repetitive tasks

  • Build powerful one-liners without writing code

The newline Guide to Bash Scripting

$39

$49