Create and Execute Bash Scripts in Linux

Shell scripts are a great way to automate repetitive tasks on Linux. You can write bash scripts that perform system-related tasks like installing software, adding new users, dynamically configuring the desktop, to name a few.

But what is the requirement? You should have a thorough understanding of the bash shell and its commands, including how to wrap those commands in a script – and most importantly – how to run the script.

Here's how you can create and run bash scripts on Linux.

What is bash scripting?

A script is a sequence of commands designed to perform a specific operation that would otherwise be performed manually by a user. In general, the commands in a script refer to a shell, and the umbrella term for writing such scripts is "shell scripting".

Linux offers a variety of shells that users can install and configure. The best known are Bash, Zsh, and Fish; Writing a script with bash commands is known as bash scripting.

Aside from bash scripts, there are several other types of scripts. For example Python scripts, Ruby scripts, Perl scripts, PHP scripts, etc.


How to create a bash script on Linux

If you want to learn how to write bash scripts on Linux, all you need is a text editor (and a pinch of perseverance).

Create a new file

To start scripting, create a new file with an ".sh" extension. This is very easy using the touch command.


Open the newly created file with any text editor of your choice. It can be a GUI based editor like Visual Studio Code or a terminal based editor like Vim or nano.

To edit the file using Vim, run the following command:


Add commands

Writing a bash script is as easy as adding words to a text file. But of course you need to know which words are valid (interpreted by the command shell) and which are not.

For this guide, we'll add the following code to the script that will display your username and the specified string when it runs.

#! / bin / bash
echo $ USER
Echo "hello world"

When you have finished writing the commands, save and exit the file to continue.

Because a bash script is a collection of Linux commands, any command you run in the terminal can be included in the script. Some examples are find, grep, man, ls, cd, etc.

How to run the bash script

Unlike other scripting languages, Bash does not require you to install a compiler (or interpreter). Every Linux distribution comes with the bash shell by default, so it has everything you need to run your scripts.

From the terminal

The most common method of running bash scripts is by using the terminal. All you have to do is give execute permissions to the script file using the chmod command.

sudo chmod + x

Now to run the script do the following:


The output shows your username and the string "Hello World" as specified in the script file.

Using the GUI

If you've been repulsed by the idea of ​​using the command line and want a graphical approach to the entire execution process, you're in luck.

Similar to before, you must first grant execute permissions to the file. To do this, right-click the file and select properties from the context menu. Find the option that says Carry out or Is executable and check the box next to it or check it depending on what desktop environment you are using.

Then just double-click the script file and choose Run in the terminal or a similar option to run the bash script.

The power of bash scripting

Most command line utilities in Linux use scripts, which are a series of commands executed in a specific order to perform a specific task. Knowledge of shell scripting can expand your technical skills and make it much easier for you, as a Linux power user, to automate tasks.

What is shell scripting and why should you use it?

In addition to accepting and executing commands interactively, the shell can also execute commands stored in a file. This is known as shell scripting. Here we cover the basics of shell scripting.

Continue reading

About the author

Deepesh Sharma
(101 published articles)

Deepesh is Junior Editor for Linux at MUO. He writes informational guides on Linux with the aim of providing a blissful experience for all newbies. I'm not sure about movies, but if you want to talk about technology, he's your type. In his spare time he can be found reading books, listening to different genres of music, or playing guitar.

From Deepesh Sharma

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive deals!

Click here to subscribe

Leave a Reply

Your email address will not be published. Required fields are marked *