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).
- Getting to know why making a Bash script executable is necessary.
- Learning how to make a script executable using both Terminal & GUI.
- Getting a comparative overview of both of the methods.
What Does it mean to “Make a File Executable” in Bash?
In Bash, ‘make 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.
When You Don’t Make Your File Executable in Bash
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 (the user) have no execution permission for the script, yet I tried to run the script & the output is showing 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.
Method 01: 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.
Steps to Follow >
➊ 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:
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!”.
Method 02: 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.
Steps to Follow >
➊ 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.
In this article, I showed you how to add executable permission to your Bash scripts using both GUI & terminal. Anyway, check the following table where I have comparatively analyzed the pros & cons of both methods.
In summary, both methods have their own pros and cons. If you have good CLI knowledge and feel more comfortable using it over GUI, use method 1. On the other hand, if you are not sure about commands or facing unknown errors, then use method 2.
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
- 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]