BASH Guide

Learn to Script with BASH!

bash logo

BASH is a shell and a scripting language that can be used with that shell and the system, it's immensely powerful for use on Linux and other Unix-like systems. Learning how to script in BASH is very useful, what you use it for varies by person but at the base of things it makes task completion more consistent, a way of documenting that process, at times can make a task quicker to complete if you do it often.. and so forth.

I like to think of BASH as an extension of GNU core ultils (of course not always just GNU utils) and userland tools, You use it with all of those utilities you would commonly find on a system (or something else if you must have a particular program need) and craft a script that will perform the sequence of commands with some program logic to control the flow. So you are not doing anything too foreign if you are already familiar with the command line, you are merely using BASH to string all those commands together to achieve what you are wanting to do.

BASH itself is actually relatively small, its builtins, functions, expansions, redirection, and so forth consist of a moderate sized portion of what you actually do in a script. You certainly use them and need to know them, but a lot of what you're actually using are GNU core ultils and userland tools. An example is you might make a script to backup a portion of your system and you might use rsync to do so. Well you can interact with and script everything that needs to accomplish that task, rsync being the bit to move the data from source to destination but using common Linux commands such as mount to mount another drive to accomplish other necessary bits. So as I said before, you're using BASH to string all these things together in a sequence to accomplish your task the same way whenever you run your script.

The best way I've found to learn programming/scripting languages is to find something you want to do, automate, create, basically a project. Even better if it is something that is interesting or useful to you in some way such as saving time by automating a task. By getting thrown right into it you're forced to learn what you need to achieve the task at hand at a more accelerated pace. Then by doing this a couple times you're coming across new things to learn and reinforcing things you've came across already, creating a well rounded base knowledge.


Along with being able to create very useful scripts you can manually run, you can also run scripts on a schedule through something called Cron similar to using Window's Task Scheduler.

You can also script tasks to be launched on boot and such as well by linking them into /etc/init.d and use rc-update if you're on Gentoo to add them to the boot run level to be ran during boot. There are also other run levels such as on shutdown, you can read the man page to get an idea of what else is possible.

ln /home/<user name>/scripts/login_script.sh /etc/init.d/example_script
rc-update add example_script boot

For systemd systems this method will not work and will be a bit different, you can find the necessary information to do the equivalent on systemd here.


There's an abundance of resources available, I'll be listing a bunch here for reference. These are resources I have used myself, so I am sharing them here as others will likely find them as useful as I have when they go to write scripts of their own.

Guides and other important references

  1. The basics of shell scripting

  2. Wooledge BASH guide

  3. Wooledge BASH guide version 2

  4. BASH Frequently Asked Questions

  5. Bash Reference Manual

  6. Bash Pitfalls

  7. The Bash Hackers Wiki

  8. tldp: Bash Guide for Beginners

  9. tldp: Advanced Bash-Scripting Guide

  10. bash-handbook

Style Guide

  1. Scripting with style

  2. Google: Shell Style Guide

  3. Bash scripting tutorial: A coding style guideline

While many people have varying opinions of which style you should code in including how variable names should be formatted (camel case?), whether you should double space or use tabs, etc. it really comes down to being as clean and descriptive as you can, indent anything that's nested, comment your functions, etc.

The purpose of this and the base importance everyone is trying to portray is that you should have a consistent and good style that will make your code readable and maintainable whether by you or someone else in the future. This increases your code quality as well, quality code should be a goal.

Books

  1. Mastering Unix Shell Scripting, 2nd Edition

  2. Shell Scripting Recipes, 2nd Edition

  3. The Linux Command Line A Book By William Shotts

  4. List of books and resources

The Linux Command Line is a free book that is aimed a those newer to Linux, the command line, and BASH. It is a reasonable length at 500~ pages, yet goes into advanced topics and slowly works you to get to that point while giving you the knowledge and tools to be successful. Additionally, it is even offered for free to help the community.

Linux Command Reference

  1. An A-Z Index of the Bash command line for Linux

  2. Write down a command-line to see the help text that matches each argument

  3. GNU/Linux Command-Line Tools Summary

Miscellaneous operations

  1. Understanding Exit Codes and how to use them in bash scripts

  2. How "Exit Traps" Can Make Your Bash Scripts Way More Robust And Reliable

  3. How to Pass Arguments to Shell Script

  4. sed command 20 practical examples

  5. How do I detach a process from Terminal, entirely?

  6. Quotes and escaping

  7. Preserve formatting when command output is sent to a variable

  8. How read file line by line

  9. Perform arithmetic operations

  10. Awk

  11. How to find files in Linux using 'find'

  12. Can a Bash script tell what directory it's stored in?

  13. How do I error check a command in BASH?

  14. Can a Bash script tell what directory it's stored in?

  15. How to Pass Arguments to Shell Script

  16. General-purpose Yes/No prompt function ("ask")

  17. How to compare strings in Bash script

  18. How to add line number for a output

  19. Using the rm command

  20. Make BASH scripts globally executable

  21. Find Out If a Variable Is Empty Or Not

  22. Get last dirname/filename in a file path argument

  23. String comparisons with if-logic

  24. Assign Output of Shell Command To Variable

  25. Compare the stdout value to the value of a variable

  26. Detecting the running user

  27. Escape special characters in a string?

  28. Catching Ctrl-c in your script

  29. How can I test if a variable is empty or contains only spaces?

  30. How to check if a variable is set

  31. Why is printf better than echo?

  32. design patterns or best practices for shell scripts

Loops for, while and until

  1. Bash For Loop Examples

  2. Examples for for, while, and until

  3. Examples for for, while, and until Pt 2

  4. do-while loop in bash

  5. How to use variables in a bash for loop

  6. Infinite Loop Examples

If-clause

  1. The if-clause

Arrays

  1. How To Find BASH Shell Array Length ( number of elements )

  2. Working with 'Arrays' in BASH Scripting

Functions

  1. tldp BASH intro - Functions

  2. Returning a string from a function

  3. Local variables for use in functions

Functions are important if you find yourself reusing the same bits of code over and over through out your script. You can put it in a function and instead replace that repeated code by the functions name which will call it, think of it as a direct substitution. An important bonus of this is if you are repeating that same code, if you ever need to change it you change it in one place not many.

Videos

  1. Introduction to Advanced Bash Usage - James Pannacciulli

Reference Cards

  1. TLDP Reference Cards

Bash Criticism

  1. Bash Weaknesses

Useful snippets or functions

  1. General-purpose function to ask Yes/No questions in Bash

  2. Read a stream from stdin and escape characters in text that could be interpreted as special characters by sed

  3. Get a scripts running directory

  4. When an error occurs the program exits with a notification displaying the exit code and line that the fault occurred at

  5. Bash One-Liners

  6. Daultons BASH functions