Welcome back to my series, "Mastering Linux Basics for Cloud and DevOps." If you've ever found yourself repeating the same commands in the terminal or you're looking for ways to streamline your tasks on a Linux system, you're in the right place. Bash scripting is like giving your computer a to-do list; it can take care of all those repetitive tasks for you, and much more.
Whether you're just getting started with the command line or you're looking to deepen your understanding, this post will walk you through everything you need to know about Bash scripting. We’ll start with the basics, like setting up your environment and writing your very first script. As we progress, we’ll dive into more advanced topics that can help you automate tasks, manage files, and even improve the security of your systems.
Bash, short for Bourne Again Shell, is a command-line tool that's available on most Unix-based systems, like Linux and macOS. It's incredibly powerful, letting you string together commands, set up loops, and manage conditions; all within a simple text file. Whether you're managing servers, deploying applications, or just want to make your daily tasks easier, learning Bash scripting will give you the tools you need to get things done faster and more efficiently.
1 . Create a New Script File
First, open your terminal. Then, you’ll use the touch
command to create a new file. Let’s call it test.sh
, but feel free to give yours a different name if you prefer:
What we just did was create an empty file named test.sh
. Pretty simple, right?
2 . Write Your First Script
Next, we need to add some content to our script. To do this, we’ll open the file in a text editor. In this case, we’re using vim
, but you can use any text editor you’re comfortable with, like nano
or gedit
:
Inside the editor, let’s add some commands to the script. We’ll start by printing a simple message to the terminal. Here’s what your script might look like:
Let’s break this down:
The
#!/bin/bash
at the top is known as a "shebang." It tells the system to use Bash to run the script.The
echo
command is used to print text to the terminal. In this case, it will print "setup and configure server."
3 . Make the Script Executable
Before we can run our script, we need to make it executable. This step is crucial, as running the script without this permission will result in a "Permission denied" error.
Based on the ls -l
output in the screenshot below, it looks like the test.sh
script currently has the permissions -rw-rw-r--
. This means the file is readable and writable by the owner (rabiatu
) and the group, but it does not have execute permissions, which is why I encountered the "Permission denied" error when trying to run it.
Explanation of the Permissions
-rw-rw-r--
:-
: Indicates it's a regular file.rw-
: The owner (rabiatu
) has read and write permissions.rw-
: The group (rabiatu
) also has read and write permissions.r--
: Others (everyone else) have read-only permissions.
In the screenshot above, there is no x
(execute) permission for any of the users, which is why I can't run the script.
To add execute permissions for the script, we’ll use the chmod
command.
Here’s a quick breakdown of what this command does:
sudo
: This runs the command with superuser (root) privileges, which is sometimes necessary for modifying file permissions.chmod
: This is the command used to change file permissions.u+x
: This option adds (+
) execute permissions (x
) for the user (u
), which in this case means the owner of the file.test.sh
: This is the name of the script file we’re modifying.
After running this command, your script will have the necessary permissions to be executed.
4 . Run Your Script
With the execute permission in place, we can now run our script by typing.
If everything is set up correctly, you should see the following output in your terminal.
Congratulations! Your script ran successfully, printing the message to the terminal just as we intended.
Running your first Bash script is a significant milestone, especially when you see it work just as expected. By making sure your script has the right permissions and is properly executed, you're on your way to mastering Bash scripting, which is a powerful tool for automating tasks and managing servers.
We’ve only scratched the surface of what’s possible with Linux and DevOps. Stay tuned for more insights and don’t hesitate to leave your thoughts or questions below!