How to create and run bash scripts on 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. To the 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.

touch script.sh

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:

vim script.sh

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.

Here’s 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 script.sh

Now to run the script do the following:

./script.sh

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 easier for you as a Linux power user to automate tasks.

Related Posts