Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoneybun committed Dec 6, 2023
1 parent a0fd39d commit 82ed5fc
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions content/bash-scripting-basics.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: BASH Scripting Basics
title: Bash Scripting Basics
description: >
Creating scripts using BASH.
Creating scripts using Bash.
keywords:
- bash
- script
Expand All @@ -14,13 +14,13 @@ section: about-your-os
tableOfContents: true
---

We will be writing a script for [BASH (Bourne Again Shell)](https://en.wikipedia.org/wiki/Bash_(Unix_shell)). There are many different languages and tools to create scripts for your tasks. Some languages are better suited for specific tasks than others. BASH is a great starting point, as it uses the same language as your built in terminal commands that already exist in your Linux OS.
We will be writing a script for [BASH (Bourne Again Shell)](https://en.wikipedia.org/wiki/Bash_(Unix_shell)). There are many different languages and tools to create scripts for your tasks. Some languages are better suited for specific tasks than others. Bash is a great starting point, as it uses the same language as your built in terminal commands that already exist in your Linux OS.

**NOTE:** This tutorial assumes you are running Pop!_OS or Ubuntu, but the script will work on any Linux/Unix OS with BASH.
**NOTE:** This tutorial assumes you are running Pop!_OS or Ubuntu, but the script will work on any Linux/Unix OS with Bash.

## Creating a working script

There are 2 important details for writing a BASH script that make it functional. The first is called the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)). The shebang tells your OS what scripting language is being used (in this case, we are using BASH). The shebang is usually the first line in the script.
There are 2 important details for writing a Bash script that make it functional. The first is called the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)). The shebang tells your OS what scripting language is being used (in this case, we are using Bash). The shebang is usually the first line in the script.

Open your text editor of choice (I will be using <u>gedit</u>, as it comes with Pop!_OS by default). In the text editor, add the following line to as the very first line in your script:

Expand All @@ -30,7 +30,7 @@ Open your text editor of choice (I will be using <u>gedit</u>, as it comes with

The line above is the shebang. There are many ways to create scripts, and the shebang will be crucial for the OS to handle your script correctly.

Let's create a directory to save your scripts. This will keep your Home directory tidy, and also keep a dedicated place for things you are working on. In my case, I have chosen to create a directory named `scripts` in my Home Directory. Next, let's save our script that we are writing to this newly created `~/scripts/` directory. You can name this script whatever you like, but for the sake of this tutorial, I am going to name this script `basics.sh`. Note that I gave the file a `.sh` extension. This is not necessary for the script to work, but it will help you as a user know that this is a BASH script because it ends with `.sh`. Other scripting languages use similar naming conventions (Python scripts end with `.py`, or LUA scripts end with `.lua`).
Let's create a directory to save your scripts. This will keep your Home directory tidy, and also keep a dedicated place for things you are working on. In my case, I have chosen to create a directory named `scripts` in my Home Directory. Next, let's save our script that we are writing to this newly created `~/scripts/` directory. You can name this script whatever you like, but for the sake of this tutorial, I am going to name this script `basics.sh`. Note that I gave the file a `.sh` extension. This is not necessary for the script to work, but it will help you as a user know that this is a Bash script because it ends with `.sh`. Other scripting languages use similar naming conventions (Python scripts end with `.py`, or LUA scripts end with `.lua`).

The second crucial detail for your OS to properly run your script is to make it executable. As of right now, your script is just a regular text file. In order for your OS to run your script, we will need to make this text file executable. This can be done by navigating to our `~/scripts/` directory with our File Manager. Once you locate your script file, right click on the file to bring up with context menu and select "Properties". This will bring up the various details about the file we are looking at. Click on the tab labelled "Permissions". There will be a checkbox labelled "Allow executing file as program". Make sure to check this box. Once this is enabled, the script will be able to run.

Expand All @@ -56,7 +56,7 @@ If it worked, your terminal should output the following line:

`The script executed correctly.`

Congratulations! You have created a working BASH script. Of course, outputting a single line does not provide much functionality, so we will be doing just that in the next section.
Congratulations! You have created a working Bash script. Of course, outputting a single line does not provide much functionality, so we will be doing just that in the next section.

## Adding functionality

Expand Down Expand Up @@ -126,7 +126,7 @@ echo "Guess the number:"
read guess
```

We now need to create and `if` statement to compare the `targetNumber` to the user input `guess` variable. The script will output different sentences depending on if the user is too low, too high, or if they guess correctly. Add the following lines to your script:
We now need to create and `if` statement to compare the `targetNumber` to the user input `guess` variable. The script will output different sentences depending on whether the user is too low, too high, or if they guess correctly. Add the following lines to your script:

```bash
if [ $guess == $targetNumber ]
Expand All @@ -147,7 +147,7 @@ If you run the script, it will ask the user for their name and their favorite co

## Conclusion

BASH scripting can be very useful in many different cases. I use scripting to automatically create a new work note with the current date as the title of the note document. I also have scripts to open certain applications or change my desktop theme. I have developed scripts to convert audio files to a specific format, or fill out PDF documents quickly. There is a lot to learn, so don't get discouraged or frustrated. Try to keep at it. As you make mistakes, you will learn for the next script you write. There are so many resources to help with your project. I have listed a few places to help with this below.
Bash scripting can be very useful in many different cases. I use scripting to automatically create a new work note with the current date as the title of the note document. I also have scripts to open certain applications or change my desktop theme. I have developed scripts to convert audio files to a specific format, or fill out PDF documents quickly. There is a lot to learn, so don't get discouraged or frustrated. Try to keep at it. As you make mistakes, you will learn for the next script you write. There are so many resources to help with your project. I have listed a few places to help with this below.

## Useful places to find information

Expand Down

0 comments on commit 82ed5fc

Please sign in to comment.