How to Create and Run a Shell Script in Debian 10

Create and Run a Shell Script in Debian 10

A shell script is a file containing a series of commands in order of their execution. When the shell script is executed, it runs all the commands one by one in order. A shell script can be executed multiple times hence it saves the time of executing every single command one by one.

Some of the reasons why you need shell scripts are as follows:

  • To avoid repetitive work
  • To automate frequently performed operations
  • Combine complex and repetitive sequences of commands into a single, simple command.

In an earlier post, we have explained creating and running the shell script in Ubuntu. This post is about how creating and running a shell script in Debian system. We will use the bash shell which is the default shell in Debian.

Note: The steps explained in this post have been tested on Debian 10 (Buster) system.

Step 1: Creating the Shell Script

To create a shell script, you only need to create a plain text file using any text editor like Nano, Vim, Gedit, etc. Then in the text file, you will need to add the commands in the same sequence you want them to be executed. Remember following points when creating a shell script:

  1. Begin the shell script with “#!/bin/bash”
  2. Each line is treated as a command
  3. Start a comment line with #

We will be creating the script named “testscript” using the Nano editor; however, you can choose any editor of your choice. Before creating a script file, make sure its name does not conflict with the commands. You can check this by typing “which” followed by the script name:

$ which script_name

In our example, it would be:

$ which testscript

The following output shows there is no such command or program installed with the name “testscript”. Therefore, we can proceed with this name.

check script name

In order to create the script named “testscript”, run the following command in Terminal:

$ sudo nano

After running the above command, an empty file will be opened in the Nano editor. First, add “#!/bin/bash” at the beginning of the file. Now add the commands you like to run in the script with each command in a separate line. Here we want to create a script that can do the following tasks:

  • Print the IP address of our system
  • List files in the Downloads directory
  • Backup files from Downloads directory to the Documents directory

So, we have written the following lines of commands in our script:


echo "Now we are going to print IP address"

ip addr | grep ens37

echo "Now we are going to ping Google website"

ping -c 2

echo "Now we are going to backup our Downloads directory to Documents directory"

cp -rv ~/Downloads ~/Documents

After entering the commands in the script, save and close it.

This is how our script file in Nano editor looks like:

shell script in Nano editor

Step 2: Set Execute Permissions

Now you will need to make the script file executable in order to run it as a program. You can so by running the following command in Terminal:

$ chmod +x script_name

In our example, it would be:

$ chmod +x

Step 3: Running the Shell Script

After the execute permissions have been applied to the script, you can run the script as follows

$ ./script_name

Note: If your script has been placed in some directory other than the current working directory, make sure to navigate to that directory first and then execute the script.

For instance, to run our script named “”, the command would be:

$ ./

After running the script, the shell will execute each command contained in the script in a sequence and will display the output. Following is the output of our script looks like:

Other than running the script using the ./script_name command, there are two more ways you can run the script, which is as follows:

$ bash script_name

$ sh script_name

This is how you can create and run a shell script in Debian 10 Buster system.

Shell scripts are just a series of commands that you add in a file and run them together. After following this post, we hope now you can easily create a shell script and automate repetitive jobs.

Similar Posts