Bash scripting is an extremely useful and powerful part of system administration and development. It might seem extremely scary the first time you do it, but hopefully this guide will help ease the fear.
Bash is a Unix shell, which is a command line interface (CLI) for interacting with an operating system (OS). Any command that you can run from the command line can be used in a bash script. Scripts are used to run a series of commands.
Bash is available by default on Linux and macOS operating systems.
This is not meant to be an extensive guide to bash scripting, but just a straightforward guide to getting started with making your first script, and learning some basic bash syntax.
Note: Newer macOS installations (from Catalina) come installed with zsh (Z shell) as the new default, but everything in this article will still be applicable.
This guide was created on macOS, and will be using /Users/you
as the default user directory for all examples. However, the concepts here will apply to any Unix-like operating system, including macOS and various Linux distributions.
In this tutorial, we’re going to:
Making a bash script is a lot simpler than you might think.
Create a file called hello-world
, using the touch
command.
touch hello-world
Edit the file with the program of your choice. Within the file, print a string that says "Hello, world!’ using echo
.
hello-world
echo "Hello, world!"
Now from the command line, run the script using the bash
interpreter:
bash hello-world
You’ll see the script has run successfully from the output.
Hello, world!
That’s it, you’ve created your first script!
#bash #command line #devops #reference #tutorial