Bash Scripting for Beginners

Bash Scripting for Beginners

If you're just starting with Bash scripting, don't worry - you're in for a fun ride! In this post, we'll break down everything you need to know to get up and running. We'll cover the basics of Bash script writing. By the end of this post, you'll be ready to write your own scripts and automate all sorts of tasks. Ready to get started? Let's go!

What is a Bash script, and why would you want to use one?

A Bash script is a set of commands that can be run together to automate a task. You might want to use a Bash script if you find yourself running the same commands repeatedly. Or if you need to modify a set of existing commands slightly for different circumstances.

For example, let's say you want to create a weekly backup of your user directory. You could write a Bash script that zips the directory, copies it to an external hard drive, and then deletes the original. Then, you could run the script every week, and your backup would be taken care of automatically.

As another example, let's say you have a group of text files that all need to be slightly modified - perhaps you need to add your name to the end of each file. You could write a Bash script to open each file in turn, make the modification, and save the file. This would be much faster than doing it by hand and less likely to result in errors. So, as you can see, Bash scripts can be handy for automating time-consuming or repetitive tasks.

How do you create a Bash script file on your computer?

To create a Bash script file on your computer, open a new text document in your favorite text editor. Then, type #!/bin/bash at the top of the file without leaving any space between the # and the rest of the characters. This is called the "shebang" line, and it tells your computer that this file should be run with the Bash interpreter.

Next, add the commands you want to run, one per line. When you're finished, save the file with a ".sh" extension. For example, if your script is called "cookies," the file's full name would be "cookies.sh".

Congratulations! You've just created your first bash script.

image.png

How do you run a Bash script on your computer?

To run a Bash script, you must first make the file executable. This can be done by navigating to the file in the terminal and running the chmod +x command. Once the file is executable, you can run it by typing ./filename.sh. The ./ tells the terminal that the file is in the current directory, and the sh extension indicates that it is a Bash script.

When you run a Bash script, the commands inside the script will be executed one by one. So, if you have a script that prints "Hello, world!" to the console, that's what you'll see when you run the script.

image.png

Basic Bash scripting commands that you'll need to know

First, let's talk about variables. A variable is a way of storing information for use later on in your script. To create a variable, you just need to use the varname=value syntax. When creating a variable, there must not be a space between the variable name, the assignment operator and the assigned value. For example, if we wanted to create a variable called MYVAR and give it the value "Hello world," we would do this:

                        MYVAR="Hello world"

Now that we've created our variable, we can print it out using the echo command. echo will simply print out whatever text you give it, so if we wanted to print out our MYVAR variable, we would do this:

                         echo $MYVAR

Next, the sleep command can be used to pause the script for a specified amount of time. This can be useful for giving the user time to read a message or perform an action.

Finally, the exit command immediately terminates the script. This is useful for dealing with errors or unexpected situations. By familiarizing yourself with these basic commands, you'll be well to becoming a Bash scripting expert.

image.png

Bash scripting can be a little daunting at first, but with a few simple commands, you can start to do some amazing things. In this post, we've shown you how to get started with bash scripting and provided a few examples of what you can do. Be sure to check out our other posts on bash.