Bash Scripting Basics
Turn repetitive commands into reusable scripts with variables, conditionals, loops and functions.
If you find yourself typing the same three commands over and over, it is time to write a script. A Bash script is just a list of commands in a file — with superpowers: variables, conditions, loops and functions.
This tutorial takes you from your first #!/bin/bash shebang to a small backup script you can actually run. No previous programming experience is required, only comfort with the command line.
The best practice
Build every example yourself. Create the files, make them executable, and run them. Scripting only clicks when you type it.
By the end of this tutorial you will be able to:
- Write and execute your first Bash script
- Use variables, arguments and command substitution
- Branch with
ifand loop withforandwhile - Write a reusable backup script
Advertisement
Tutorial Lessons
- 01 Your First Bash Script The shebang, making a file executable, and running it. 2 min
- 02 Variables and Arguments Store values, use command substitution, and read command-line arguments. 2 min
- 03 Conditionals and Loops Branch with if and test expressions, and repeat work with for and while loops. 2 min
- 04 Functions and a Real Backup Script Define reusable functions and combine everything into a working backup script. 2 min
Advertisement