If you are new to the world of Linux, there are many ways to execute files and scripts. With a little practice, you can easily create your own scripts that help automate everyday tasks. In this guide, you will learn step by step how to create and execute a simple Bash script. You will learn how to use terminal commands and the basics of Linux permissions.

Key Insights

You will learn how to:

  1. Create and save a script.
  2. Set execution permissions for the script.
  3. Execute the script and move it if necessary.
  4. Perform actions safely and efficiently.

Step-by-Step Guide

1. Creating a Bash Script

To start creating your first Bash script, first open your terminal. You can create a new script using the text editor Nano. Enter the following command:

nano helloworld.sh

The above script is called helloworld.sh. To ensure that the interpreter, in this case Bash, is used, add this line at the beginning of your script:

#!/bin/bash

Next, you can enter your first command. For example:

echo "Hello World"

If you save and exit the script now, you will have created your first script file.

Execute Bash Scripts - Beginner's Guide

2. Setting Permissions

The next step is to give the script permission to be executed. This is done with the chmod command. Enter the following command:

chmod +x helloworld.sh

This command sets the execution permission for the script.

Run Bash Scripts - Beginner's Guide

To ensure that the permission has been set successfully, you can type ls to list the files in the directory. Your script should now be displayed in a different color, indicating that it is executable.

3. Executing the Script

To execute your script, navigate to the directory where the script is located. You can execute the script with a dot and a slash (./):

./helloworld.sh

This should display the output “Hello World” in the terminal.

Execute Bash Scripts - Beginner's Guide

4. Using the Full Path

If you want to ensure that your script can also be found via the full path, you can specify the full path to the script. For example:

/home/yourUsername/documents/helloworld.sh

Replace “yourUsername” with your actual username. This way, the execution of your script will work regardless of the directory you are in.

Execute Bash Scripts - Beginner's Guide

5. Moving Scripts

A useful method for executing scripts is to move them to a directory included in the PATH, such as /usr/local/bin. To do this, you may need to use root permissions. You can move the script with the following command:

sudo mv helloworld.sh /usr/local/bin/

This requires you to enter your password. Once moved, you can execute your script from anywhere by simply typing the name of the script.

Run Bash Scripts - Beginner's Guide

6. Deleting the Script

If you no longer need your script, you can delete it with the rm command. Don’t forget to use root permissions with sudo if you have moved it to a system-wide directory:

sudo rm /usr/local/bin/helloworld.sh

Deleting scripts is an important step in keeping your environment clean.

Execute Bash Scripts - Beginner's Guide

Summary – Executing Files in Linux

Now you have learned the basics of creating and executing a Bash script in Linux. From creating your own script to executing and deleting it, you have gone through all the essential steps. With this knowledge, you are well-equipped to delve further into script programming.

Frequently Asked Questions

How do I create a simple Bash script?Open the terminal and use a text editor like Nano to create a new file with the.sh extension.

How do I set execution permissions for a file?Use the command chmod +x filename.sh in the terminal.

How do I execute a Bash script?You can execute a script with./filename.sh when you are in the directory of the script.

Can I move a script to another directory?Yes, use the command mv filename.sh /destination-directory/.

What do I do if the script is not found?Ensure that the script is in the current directory or provide the full path.