Skip to main content

13 resources for learning to write better Bash code

Whether you're new to writing scripts or you've been using Bash for years, there's always something to learn. Boost your scripting skills with these guides, tutorials, and examples.
Image
letter tiles spelling learn

Image by Wokandapix from Pixabay

Learning a scripting language is an intimidating task. It also takes time because practice is the only way to master a skill properly, and you will need to do and redo your code to learn new techniques and fix your mistakes.

By the end of this article, you will have:

  • A list of online tutorials to sharpen your skills
  • A reference guide for Bash scripting
  • Best practices to write code and make it more secure
  • Resources to edit and execute Bash scripts when you don't have access to a Linux machine

This is not a complete guide, but it will serve as a starting point to more resources.

Online tutorials

Nothing beats a detailed explanation from someone more experienced, especially if it includes examples. Here are some tutorials to check out:

  • Bash scripting tutorial for beginners: Linuxconfig.org's tutorial is ideal if you want to quickly learn the basics with examples, divided by topic.
  • Learn Shell: This interactive Shell Programming tutorial ranges from basic to advanced skills with lots of examples.
  • Enable Sysadmin: The website you're on has many practical articles (like this one). You may find a few of mine here, with open source code.
  • FreeCodeCamp: These tutorials cover Bash and many other languages. Be prepared to spend days browsing this one.

Reference guide

Sometimes you want to learn how to perform a specific task without distractions. To learn the official way of solving things, as they say, RTFM (read the fine manual):

  • GNU Bash reference manual: This is the bible of Bash and includes concise examples, organized by topic. You will keep coming back to this guide over and over.
  • Also, if you are on Linux, learn to use these two commands:
    # 'The GNU Bash Reference Manual', for 'Bash'
    info bash

    And:

    # bash - GNU Bourne-Again SHell
    man bash

[ Another one to add to your list: A sysadmin's guide to Bash scripting. ]

Check your Bash code

You want to learn how to write efficient code, avoid common mistakes, and make sure your scripts are secure. Fear not, there are plenty of resources out there to learn basic and advanced skills, including:

  • ShellCheck: I recommend installing this shell script static analysis tool. You can use it online, but its true power comes when you integrate it with Vim or IntelliJ IDE. You may wonder how you managed to live without it for so long.
  • OWASP command injection page: Command injection is commonly used to elevate privileges on scripts or programs that do not sanitize user input. The Open Web Application Security Project (OWASP) is an international nonprofit that tracks security vulnerabilities to help people avoid repeating them. I find it helpful to be familiar with this list of risks, like the infamous Shellshock, so I don't repeat them in my scripts.

Code repositories

The following repositories contain Bash scripts written so that you can learn specific topics by checking the code. All the code is open source, and you can copy and paste the examples to learn.

  • Complete Bash shell scripting: This is a nice code repository published by Packt with examples organized by topic.
  • Udemy practice script repository: What could be better than learning about Bash programming by example? This repository from Udemy's course on shell scripting is nicely organized by topic. Each script topic is small and self-describing.
  • shell-scripting-examples: This is a cookbook of common tasks in Bash (like reading from a property file or accessing a MySQL database). These examples are well worth checking out.

Keep in mind that code in repositories like GitHub may contain bugs. Also, be very careful not to download and run code as the superuser, or you may find yourself in a pickle.

[ Don't miss Stupid Bash tricks: History, reusing arguments, files and directories, functions, and more. ]

Run code online

What if you want to try something but you don't have access to Linux or Unix? This is less than ideal, but you can try a few online resources with very nice front ends to run your scripts, such as:

  • MyCompiler.io: This site provides syntax support, lets you run code from your browser, and supports multiple languages. It's very convenient, especially if you don't have access to Linux to test a simple script
  • Bash online compiler: You can run Bash scripts directly from your browser, with a few restrictions, on this site. It also has good syntax color support.
  • LeetCode: This site has a nice playground where you can write scripts in Bash (and many other languages). If you want, you can try to solve some of its challenges (with paid and free subscription options).

Become a better programmer

These online resources can help you become a better Bash programmer. Some other things to keep in mind:

  • Learn basic concepts by looking at the online tutorials but always check the reference guides. They are the most trustworthy (well, almost always).
  • Avoid common mistakes by checking the ShellCheck and OWASP websites. You will thank yourself later.
  • Learn by example by looking at how others solve common problems (in places like GitHub). Also, if you find a bug, contribute and report it or, even better, submit a patch.
  • Don't forget that practice is the best way to learn something, so go ahead and start writing a few scripts!
Topics:   Bash   Scripting   Career   Programming  
Author’s photo

Jose Vicente Nunez

Proud dad and husband, software developer and sysadmin. Recreational runner and geek. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.