Skip to main content

Learn Bash Scripting

Chapter #2: Writing and Running Your First Bash Script

In this lesson, we’ll walk you through creating your very first Bash script, running it, and learning some helpful terminal tips along the way.

The bash script is a powerful scripting language that every Linux user either wants to master for Linux administration or just as a regular developer needs to know in order to make managing Linux easy and automated.

At first, you may think that it is hard to learn as there are a lot of commands and symbols within the language itself, but in reality, Bash is easy and fun to learn, as you will see the results from the first attempt to learn, unlike other programming languages.

You won't get what you really need to use in other programming languages until a certain point init learning. On the other hand, in Bash, every command you learn gives you an immediate result, and you remember why you need it.

All you need as a beginner is to follow along with every step to get the most benefit. Each command or trick will make your life easier and help you become more productive.

In these lessons, let's learn how to write our first Bash script file, how to execute it, and get some tricks and tips that make writing commands in the terminal simpler and more efficient.

How to Write a Bash Script

We already learned that a Bash script file is just a file where we put a sequence of commands that are combined together to achieve a specific role or task.

Don't be afraid of using the terminal or the shell. Every time you write a command, you will become more familiar with it and see how simple and fun it is.

Let’s now create our very first Bash script!

Step 1: Open the Terminal

The first step is to open your terminal, either using the graphical way by searching for the terminal in the application menu of your distribution or by using the simple key binding Ctrl + Shift + T. It is highly recommended to start using keybindings instead of the mouse, as this will make you faster at performing tasks.

The terminal window will sometimes look different, but how it looks is not really important. The commands are the same; it's just some color and font customization.

Step 2: Create a Working Directory

To keep our work organized during this whole series of learning Bash, we will create a folder, often called a directory in Linux. The command to create a new directory in Linux is mkdir, which stands for "make directory", followed by the name you want, like this:

Here, I create a directory called bash-series, where we will put all our Bash files and code that we write during this journey.

mkdir bash-series

After creating the directory, we need to create our first Bash file. First, we navigate to the directory that we just created using the command cd, which stands for "change directory".

As mentioned, Unix-like systems use the term "directory" instead of ",," but don't be confused; they are the same. The command is followed by the name of the directory, like this:

cd bash-series

Step 3: Write Your First Bash Script

Now, you are inside the directory, and everything you create will be inside it. Let's create our first Bash file using the touch command, followed by the file name, like this:

touch first.sh

Here, I created a file called first.sh, and you should pay attention to the .sh extension, which is important because it tells Linux that this file is a Bash script and not a normal file.

Now we are ready to start adding some Bash content to this file. Depending on what text editor you use, this is not a problem; you can use whatever you want.

If you use Vim, run the command vim first.sh to open the file in the text editor. If Vim is not installed, you can also use vi.

vim first.sh
OR
vi first.sh

Then, add the following line:

echo "Bash Guide with pro.tecmint.com"

This line simply prints the message "Bash Guide with pro.tecmint.com" to the screen using the echo command, which outputs the message we write.

To save the file and exit when using vi or vim, press the Esc key to ensure you're in normal mode, then type :wq and hit Enter. This command tells the editor to write (save) the file and quit.

Step 4: Run Your First Bash Script

The last step is to learn how to run our script in the terminal and get the expected result.

When we first create the file, Linux treats it as a simple file, meaning we cannot run it directly. We need to make it executable first.

To do this, we use the chmod +x command, which stands for "change mode"; it modifies the file's permissions. The +x flag makes the file executable. The command should be followed by the file name like this:

chmod +x firsth.sh

The final step is to run the script, which we do as follows:

./first.sh

As a result, we get the message:

Bash Guide with pro.tecmint.com"

Is that the only way to run a Bash script in Linux? No, there are other ways. If you don’t want to make the file executable before running it, you can simply call bash followed by the file name like this:

bash first.sh

This method is simpler, but you need to call bash each time before running the file. It is useful for quickly running a script and checking the result.

Writing My First Bash Script

Essential Terminal Shortcuts for Faster Editing

As a Linux user, you’ll spend a lot of time inside the terminal, whether it's for running commands, writing scripts, or editing text. Many of these tasks involve typing and adjusting commands directly in the terminal, so knowing a few keyboard shortcuts can save you time and make your workflow much more efficient.

Let’s say you type a command like this:

cho "Bash series with pro.tecmint.com"

Then you realize you misspelled echo. You’d need to move the cursor back to the beginning of the line to fix it. While you could hold the left arrow key, there’s a quicker way, just press Ctrl + A, and the cursor jumps straight to the start of the line.

Want to go to the end of the line instead? Press Ctrl + E.

To move backwards by one word at a time, use Alt + B. To move forward by a word, use Alt + F.

Need to delete a word? Place the cursor at the start of the word and hit Alt + D.

Want to delete everything from a certain point to the beginning of a word? Use Alt + Backspace.

For example, if you typed:

echo "bash series with pro.tecmint.com"

And you want to remove just pro.tecmint.com, put the cursor at the beginning of the word, and hit Alt + D.

To delete everything starting from that word to the end of the line, Alt + Backspace will do the trick.

These simple terminal shortcuts will help you write and edit commands more quickly, and make your time in the shell a lot more productive.

In this guide, we explored how Bash scripting helps make your Linux experience faster, easier, and more efficient. Whether you're managing systems or just want to automate some tasks, Bash is a must-know tool for every Linux user.