How to Run Bash Script in Terminal

Fumbani Banda Feb 02, 2024
  1. Run Bash Script With the bash Command
  2. Make Bash Script Executable With chmod
How to Run Bash Script in Terminal

This tutorial demonstrates ways to run a bash script in the terminal with the bash command and by making it executable.

Run Bash Script With the bash Command

To run a bash script in Linux, you need to use the bash command and specify the name of the script you want to run.

printf  "This is my first script.\n"
printf  "This is fun.\n"

Run the script with bash:

bash script.sh

Output:

This is my first script.
This is fun.

Make Bash Script Executable With chmod

chmod is used to modify permissions to a file. In our case, chmod adds the permission to execute for script.sh. We can now run the script by ./script.sh.

printf "This is my second script.\n"
printf "This is more fun.\n"

Make the script executable:

chmod +x script.sh

Run the script:

./script.sh

Output:

This is my second script.
This is more fun.
Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub