Categories: Bash Scripting

How To Run A Bash Script In Linux Step By Step

Introduction

Running a Bash script in Linux is a basic but important skill for anyone learning Bash scripting. A Bash script is a file that contains Linux commands. When you run the script, all the commands inside the file are executed in order.

Bash scripts are commonly used for automation, system administration, cybersecurity tasks, backups, log checking, file management, and server monitoring. If you are new to Linux, learning how to run a Bash script will help you save time and avoid typing the same commands repeatedly.

In this tutorial, you will learn different ways to run a Bash script in Linux using simple command-line examples.

Create A Simple Bash Script

First, create a new Bash script file using the nano editor:

nano test-script.sh

Now add the following code:

#!/bin/bash

echo "This is my Bash script"
echo "Current user: $(whoami)"
echo "Current directory: $(pwd)"
echo "Date and time: $(date)"

Save the file in nano by pressing:

CTRL + O

Press Enter, then exit nano:

CTRL + X

Method 1: Run Bash Script Using Bash Command

The easiest way to run a Bash script is by using the bash command:

bash test-script.sh

This method does not require execute permission. Bash directly reads the script and runs it.

Example output:

This is my Bash script
Current user: kali
Current directory: /home/kali
Date and time: Mon May 24 10:30:00 IST 2026

This is a beginner-friendly method because it works even if the script is not executable.

Method 2: Run Bash Script With Execute Permission

To run a script directly, you need to make it executable using the chmod command:

chmod +x test-script.sh

Now run the script like this:

./test-script.sh

The ./ means you are running the script from the current directory.

Method 3: Run Bash Script Using sh Command

You can also run a script using:

sh test-script.sh

However, this may not always behave the same as Bash. Some Linux systems use a different shell for sh. If your script contains Bash-specific syntax, it is better to use:

bash test-script.sh

Method 4: Run Bash Script From Any Directory

To run your script from anywhere, move it to a directory included in your system PATH, such as /usr/local/bin.

Example:

sudo cp test-script.sh /usr/local/bin/test-script
sudo chmod +x /usr/local/bin/test-script

Now you can run it from anywhere:

test-script

Common Permission Error

Sometimes you may see this error:

Permission denied

Fix it by giving execute permission:

chmod +x test-script.sh

Then run again:

./test-script.sh

Conclusion

Running a Bash script in Linux is simple once you understand the available methods. You can run a script using bash script.sh, execute it directly with ./script.sh, or place it in your system path for quick access.

For beginners, the best method is to start with the bash command. As you become more comfortable, use chmod +x and run scripts directly. Learning how to run Bash scripts is the first step toward Linux automation, cybersecurity scripting, and advanced system administration.

0xSnow

0xSnow is a cybersecurity researcher with a focus on both offensive and defensive security. Working with ethical hacking, threat detection, Linux tools, and adversary simulation, 0xSnow explores vulnerabilities, attack chains, and mitigation strategies. Passionate about OSINT, malware analysis, and red/blue team tactics, 0xSnow shares detailed research, technical walkthroughs, and security tool insights to support the infosec community.

Recent Posts

Install Samba on Ubuntu 18.04: Configure Shares and User Access

Samba is a free, open-source implementation of the SMB/CIFS network protocol that lets Linux servers share…

12 hours ago

Set Up an OpenVPN Server on Ubuntu 18.04 with EasyRSA and UFW

Running your own VPN gives you full control over your traffic, privacy, and connection security. It encrypts…

12 hours ago

Install IntelliJ IDEA on Ubuntu 18.04 via Snap: Setup Guide

IntelliJ IDEA is a full-featured IDE for JVM and Android development made by JetBrains. It includes…

12 hours ago

Install Steam on Ubuntu 18.04: Multiverse Setup and First Run

Steam is a cross-platform digital distribution platform by Valve Corporation that gives you access to thousands…

12 hours ago

Install Redmine on Ubuntu 18.04 with MySQL, Passenger, and Nginx

Redmine is one of the most popular open-source project management and issue tracking platforms. It is…

12 hours ago

Install VirtualBox on Ubuntu 18.04 from the Oracle Repository

VirtualBox is a free, open-source, cross-platform virtualization application maintained by Oracle. It lets you run multiple…

2 days ago