Shell scripting for humans

Learn-as-you-code writing bash shell scripts

This entire course is aimed at beginners, but if you’re familiar with Linux or even other operating systems, you’ll still get something out of the later sections in the course.  At the end of the course, you’ll have your own template script you can use to start creating your own.

What you’ll learn

  • How to write shell scripts in bash.
  • How to use the command line.
  • How to use vim to create files.
  • How to parse and manipulate strings and text files.
  • How to use variables.
  • How to use conditionals to take action if something is true or false.
  • How to write loops to repeat a process.
  • How to understand logic and programming.
  • How to write a command line utility that accepts arguments and options.

Course Content

  • Introduction –> 6 lectures • 11min.
  • Write A Shell Script In Vim –> 7 lectures • 11min.
  • Running Your First Shell Script –> 7 lectures • 15min.
  • Exploring the command line and writing a file navigation script. –> 9 lectures • 12min.
  • Commands and how to use them –> 3 lectures • 4min.
  • Variables –> 5 lectures • 14min.
  • If/else and conditionals –> 8 lectures • 14min.
  • Loops –> 5 lectures • 8min.
  • The Pipe –> 8 lectures • 16min.
  • Some fun stuff (and quizzing your knowledge from the previous sections) –> 7 lectures • 4min.

Shell scripting for humans

Requirements

  • A Mac (preferably) or other computer that can run a bash shell.

This entire course is aimed at beginners, but if you’re familiar with Linux or even other operating systems, you’ll still get something out of the later sections in the course.  At the end of the course, you’ll have your own template script you can use to start creating your own.

You won’t find any slides on this course; it’s all hands-on work that lets you learn as you code.  After the introduction, you’ll start writing a script right away and I’ll explain each line of code, what it does, and why you want it.

Throughout the course, you will write 20 different scripts:

  • first_script will print text to the screen (created with echo)
  • second_script will print text to the screen (created with `vim`) and uses a shebang
  • debug will run verbosely with extra output
  • debug2 will run verbosely with extra output (`vim` and `chmod` practice)
  • cd will move into a folder and list it’s contents (a pseudo file browser script)
  • vars will assign and prints a variable
  • literal will assign and print a literal variable name
  • cmd_output_vars will save the output of a command in a variable and print it to the screen
  • if will do simple conditional checks
  • rename a broken script that will batch rename files using `if`/`else`/`mv` and more, bu quires you to fix it (debugging practice)
  • lowercase will use a for loop to loop through files in a directory
  • change_extension will change all files of a certain file extension to a new file extension
  • dir_args will accept a directory as an argument passed to it and list only folders it contains
  • version will print a version when the `–version` flag is passed (also practicing global and cal variable scopes)
  • usage will print a help message with `–help` or if no args are passed
  • logging will log to a file in three ways (INFO, WARN, DEBUG) using a log with the same name as the script
  • trap will show a message when you interrupt the script’s execution
  • parse_args will parse for different arguments
  • detect_os will check several operating systems and display the one it detects
  • template will be a starting point you can use for all your future shell scripts

The start of the course is intentionally slow and broken into many small lectures.  This allows you to really understand what’s going on and let you absorb what you’ve learned and then apply it to a real-world example.  In addition to learning shell scripting, you’ll also learn about macOS and Linux in general as this helps you write better scripts.

I recommend using a Mac for this course because it has everything you need to get started without having to install anything extra.  I’ll also be working on a Mac throughout the course.  But you’ll be able to run all the same scripts from a Linux machine, or even a Windows machine that runs the Windows Subsystem for Linux.  There will be some differences in those cases, but I’ll explain them as we go.

If you’re learning this for work, the first sections can get you up to speed quickly, while the later sections can help you uncover advanced topics you may need for your job.

If you’re joining the course for your personal life, you can automate some tedious tasks you may do such as renaming files, data entry, finding things, etc.

Get Tutorial