FUNDAMENTALS A Complete Guide for Beginners
Enabling the execution of Bash script files is a necessary step for running your Bash programs effectively as they aren’t executable by default. In this article, I will show you how you can make your Bash scripts executable using your system’s both Command Line Interface (CLI) & Graphical User Interface (GUI).
What Does it mean to “Make a File Executable” in Bash?
In Bash, making a file executable refers to granting the file the necessary permissions to be executed as a program. By default, when you create a file in Bash, it does not have the executable permission set. However, if you want to run that file as a script or program, you must make it executable.
Why I’ll Make File Executable?
If you don’t make your file executable, you won’t be able to run it directly as a program in Bash. Attempting to execute a non-executable file will result in a “permission denied” error. Check out the following image, where I tried to run a non-executable script: See from the permission part, I have no execution permission for the script, yet I tried to run the script & the output showed a “permission denied” error.
2 Methods to Make File Executable in Bash
Now that you know how important & necessary it is to add execution permission to the Bash script file you want to execute, let’s dive into the second part of the article. In the following, I will discuss how you can add executive permission to your scripts using both your system’s CLI & GUI.
1. Make Bash File Executable Using Terminal
In the first method, I will discuss the process using the terminal. The command that is used to add any kind of permissions to any files, is the chmod command. Go through the following steps to see how you can add the execution permission to your script:
-
At first, open your Ubuntu Terminal application.
-
Next, write the following command to add permission to your script.
chmod u+x test.sh
-
Finally, press the ENTER button.
-
Now that, I have added the permission, now let’s check by running the script using the following command:
./test.sh
Here, I used the same Bash script file “test.sh” which I tried to run at the beginning of this article, but this time I ran it after adding executable permission. As a result, the script is successfully executed & is displaying the output “Hello!”.
2. Make Bash File Executable Using GUI
In this second method, let’s check the process using GUI. Check out the following steps to see how it works:
-
First, open your File Manager, which is commonly found in the applications menu or the system toolbar.
- Locate the Bash file & right-click on it to open the context menu. Select the “Properties” option from there.
- Then, click on the “Permissions” section & select the box of the “Execute: Allow executing file as program” option at the bottom of the permissions section, and close the “Properties” window.
- After that, you will be able to execute the Bash file. For that, again Right-click on it. And the following prompt will show up. But this time there will be an option saying “Run as a program”. Either click on it to run the program or you can also open the file in your terminal to print the output in the terminal.
With these simple steps, you can add execution permissions to any of your Bash scripts.
Conclusion
In this article, I tried to discuss the process you need to learn step-by-step about making a Bash script file executable. This a must-learn topic to run a program or any of the Bash files. I showed the process using both CLI & GUI. Hope this article helps you learn to execute a Bash file.
People Also Ask
Related Articles
- A Complete Overview of Bash Dot Command [With 2 Examples]
- What are the Usages of Bash Source [3 Practical Examples]
- How to Run a Bash Script [2 Methods with Cases]
- How to Run Bash Commands in Parallel [4 Ways with Examples]
<< Go Back to Executing Bash Script | Bash Scripting Basics | Bash Scripting Tutorial