Linux Tactic

Mastering Bash Scripting: A Beginner’s Guide

Introduction to Bash Scripting

As more and more people migrate to Linux from Windows and MacOS, learning Bash Scripting becomes an essential skill. Bash is short for Bourne-Again Shell, and it is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the original Bourne shell (sh).

In this article, we will be discussing the basics of Bash Scripting and how to write a simple Bash script in Linux Mint 20. Our aim is to help beginners start writing their own scripts without feeling daunted by the process.

Bash as a Programming Language

Bash is a powerful programming language that can be used to create a wide range of scripts, from the simplest command-line tools to complex automation scripts. Bash scripts can be used to perform a range of tasks, such as automating system administration tasks, processing large data sets, and developing custom command-line tools.

One of the key strengths of Bash is its portability. Bash scripts are highly portable, and they can be run on any platform that supports a Bash shell (Linux, Unix, macOS).

This means that once you have written a Bash script, you can run it on any machine that has Bash installed.

Purpose of the Article

We aim to introduce the basics of Bash Scripting and walk beginners through the process of writing a simple Bash script in Linux Mint 20. By the end of this article, you should feel comfortable writing your simple Bash scripts, and you should be able to run them in Linux Mint 20.

Writing a Simple Bash Script in Linux Mint 20

To start writing a Bash script, you will need to create an empty document with the .sh extension. You can do this by opening the text editor on Linux Mint, typing the code to create an empty document and then saving the file with the .sh extension.

Creating a Bash File

To create an empty document, open your text editor (such as pluma, gedit, or nano) and create a new file by navigating to the File menu and clicking New. Once you have created a new file, save it with a name of your choice and with the .sh extension.

For example, you could name the file my_script.sh.

Adding the First Line of Code

To tell the terminal to use Bash to execute your script, you will need to add the following line at the beginning of your script:

#!/bin/bash

This is known as a shebang and tells the terminal which interpreter to use to execute the script.

Writing a Bash Script to Display a Message

Now that you have created a Bash file and added the shebang, you can start writing your Bash script. Lets write a simple script that displays a message when executed.

Open your Bash file in your preferred text editor and add the following line of code:

#!/bin/bash

echo “Hello World!”

The first line of code tells the terminal to use Bash as the interpreter for the script, while the second line of code uses the echo command to print the message Hello World! to the terminal.

Executing the Bash Script

To execute the Bash script, open the terminal and navigate to the directory where your script is located. Once you are in the correct directory, type the following command:

bash my_script.sh

Assuming that your script is named my_script.sh, the above command will execute the script, and you should see the message Hello World! printed to the terminal.

Conclusion

Bash Scripting is an essential skill for anyone working with Linux, and it can seem daunting to beginners. However, by following the simple steps we have outlined in this article, you should be able to write and execute your Bash script with ease.

Remember to start with simple scripts and then work your way up to more complex tasks. Good luck!

Making the Bash Script Interactive

In the previous section, we learned how to write a simple Bash script that displays a message when executed. In this section, we will learn how to make Bash scripts interactive by taking input from the user and displaying the users input.

Taking Input from the User

To make a Bash script interactive, we can use the read command to ask the user to input information. Lets modify our previous Bash script to take input from the user:

#!/bin/bash

echo “What is your name?”

read NAME

echo “Hello $NAME!”

In the above script, we have added an additional line using the read command. The read command waits for the user to input some value and stores this value in the variable NAME.

Displaying a Message to the User with Their Input

To display the users input, we can use the $ symbol to access the value of the variable:

#!/bin/bash

echo “What is your name?”

read NAME

echo “Hello $NAME! It is nice to meet you.”

In the above script, we have modified the message to include the users name. When the script is executed, the read command waits for the user to input their name.

Once the user has entered their name and pressed Enter, the script uses the $ symbol to access the value of the NAME variable and displays the message, Hello NAME! It is nice to meet you.

Adding the Date Command to the Bash Script

You can also incorporate the date command into your Bash script. The date command displays the date and time in a certain format.

Here is an example of how you can use the date command in your script:

#!/bin/bash

echo “What is your name?”

read NAME

DATE=$(date +”%Y-%m-%d”)

echo “Hello $NAME! It is $DATE today.”

In the above script, we have added a new line of code that saves the current date in a variable named DATE. The %Y-%m-%d format specifies the year, month, and day, respectively.

The script then uses the $ symbol to access the value of the variable DATE and includes it in the greeting.

Saving Bash Script Output to a File

By default, the output of a Bash script is displayed in the terminal. However, you can also save the output to a file by using the >> symbol.

Heres an example:

#!/bin/bash

echo “What is your name?”

read NAME

echo “Hello $NAME! It is nice to meet you.”

DATE=$(date +”%Y-%m-%d”)

echo “Today is $DATE.” >> output.txt

When the script is executed, the output Hello NAME! It is nice to meet you. is printed to the terminal.

The script then saves the output Today is DATE. to a file named output.txt located in the same directory as the script.

Checking the Output File in the Home Directory

To view the contents of the output file, you can open it in a text editor or view it in the terminal using the cat command:

cat output.txt

This will display the contents of the output.txt file in the terminal. If you want to delete the file, you can use the rm command:

rm output.txt

Conclusion

In this article, we learned how to make Bash scripts interactive by taking input from the user and displaying the users input. We also learned how to add the date command to our scripts and save the output to a file.

By following the steps outlined in this article, you should now be able to create simple Bash scripts that are interactive and save the output to a file. With practice, you can expand your Bash scripting skills to perform more complex tasks.

In conclusion, Bash scripting is an essential skill for anyone working with Linux, and it can seem daunting to beginners. Through this article, we have learned the basics of Bash scripting, and we have walked through the process of writing and executing a simple Bash script in Linux Mint 20.

We introduced how to make a Bash script interactive by taking input from the user, displaying the user’s input, adding the date command to the Bash script, and finally saving the script’s output to a file. With practice, you can expand your Bash scripting skills to perform more complex tasks, making your daily work streamlined and efficient.

Bash scripting may seem intimidating, but with determination and practice, you can become a master of Bash scripting.

Popular Posts