Bash scripts, also called shell scripts, are programs that help automate tasks by storing a series of commands that are commonly executed together, such as system updates or backups. Instead of typing each command manually in the terminal, you can store them in a script and run them whenever needed.
- Automates multiple commands in a single execution.
- Saves time when performing repetitive system tasks.
- Helps manage system administration and maintenance operations.
- Can be executed using several methods in the terminal.
The Shebang Line
Before running a Bash script, it's important to understand the shebang line also known as hashbang or pound-bang. The shebang is the first line of a script and specifies which interpreter should be used to execute the script. Although some scripts may run without a shebang, including it is considered best practice because it ensures the script runs with the correct interpreter across different systems.
Example:
#!/bin/bashStructure of the Shebang Line
#! /path/to/interpreter [optional-arguments]- #! : Indicates the start of the shebang line.
- /path/to/interpreter: The full path to the interpreter that will execute the script, such as /bin/bash or /usr/bin/env python3.
- [optional-arguments]: Optional flags or arguments passed to the interpreter.
Methods to Run Bash Script in Linux
Method 1: Running a Script Using the bash Command
The most common way to run a Bash script is by explicitly calling the Bash interpreter and passing the script file as an argument.
Syntax:
bash filename.sh- bash: The Bash interpreter used to execute the script.
- filename.sh: The name of the Bash script file.
Example:
bash hello.shOutput:

Method 2: Running a Script Using the sh Command
The sh command runs the script using the system's default shell interpreter. In many Linux systems, sh is linked to Bash, but it may behave differently depending on the system configuration.
Syntax:
sh filename.sh- sh: Refers to the system’s default shell interpreter.
- filename.sh: The Bash script file to execute.
Example:
sh hello.shOutput:

Running Scripts from Another Directory:
If the script is located in a different directory, you must provide the relative or absolute path to the script.
Example:
bash /home/user/scripts/script.shor
bash ../scripts/script.shMethod 3: Running a Script Using the source Command
The source command is another way to execute a Bash script. This method runs the script within the current shell environment instead of starting a new shell.
Syntax:
source filename.sh- source: Executes the script in the current shell session.
- filename.sh: The script file to be executed.
Example:
source hello.shOutput:

Note: When a script is sourced, all commands inside the script run in the current shell environment. This means any variables or environment changes made by the script will remain active after the script finishes executing.
Method 4: Making a Script Executable Using chmod
This method allows you to run a Bash script as an executable instead of explicitly passing it to the bash command. To do this, you change the file’s permissions using the chmod command. Once the execute permission is added, the script can be run directly from the terminal.
Step 1: Give Execute Permission
Navigate to the directory where your script is located and run:
Command:
chmod +x filename.sh- chmod: Command used to change file permissions.
- +x: Adds execute permission to the file.
- filename.sh: The script file.
Note: If you do not have sufficient permissions, you may need to run the command with sudo.
Step 2: Run the Script
After you've granted execution permission, you can run the script, assuming you are in the same directory as the script
Syntax:
./filename.sh- ./: Refers to the current directory.
- filename.sh: The executable Bash script.
Command:
./demo.shOutput:

Running Executable Scripts from Another Directory:
If you are not on the same path as the bash script, make sure you provide the relative path to the file or the bash script.
Syntax:
./pathToTheFile.shExample:
./shellscripts/demo.shOutput:
