How to Make a File Executable in Bash [2 Methods]

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).

Key Takeaways

  • 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:Execute permission denied 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.

Anyway, you can read the Comparative Analysis of Methods section to distinguish between these two methods and best pick one for your need.

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.

To learn, how to create, write & save a Bash script you may check out “How will you start writing a Bash 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

Add executive permission

EXPLANATION
  • chmod: Changes the permission of files and directories.
  • u+x: Argument with chmod command to add the executable permission for the user.
  • sh: File which you want to make executable.

➌ 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

Execute a bash scriptHere, 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.Open file manager ➋ Locate the Bash file & right-click on it to open the context menu. Select the “Properties” option from there.Open bash file properties ➌ 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.Select the execute permission ➍ 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. Execute the bash file 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.

Comparative Analysis of the Methods

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.

Methods Pros Cons
Method 1
  • Faster for advanced users who can execute commands quickly.
  • Easier to sort permissions among different owner groups.
  • Requires familiarity with CLI.
  • Increased possibility to make mistakes due to manual typos and syntax errors.
Method 2
  • Does not require any knowledge of commands.
  • The visual representation can help beginner-level users.
  • Slower due to clicking and navigation.
  • Does not provide the same level of functionality as the CLI.

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.

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

How to create executable file in shell?
To create an executable file first, create a file (i.e. text.sh) & open it in any text editor to write inside the script. After that save & exit the editor. Then make it executable by adding permission with the syntax: “chmod u+x test.sh”. Now you have an executable file. You can test by running it using “/test.sh”.

How do I make a chmod file executable by everyone?
To make a file executable for everyone, you can use the “chmod” command with the permission value “a+x”, which grants executable permission (denoted by ‘x’) for all (denoted by ‘a’) groups. Just use the syntax, “chmod a+x filename”.

How do I change a file to an executable in Linux?
To change a file to an executable program in Linux, you can use the chmod command to add executable permission to it. For that use the syntax: “chmod +x filename”.

How do I make a Unix file executable?
To make a file executable in Unix, you can use the ‘chmod’ command. For that use the syntax: “chmod +x filename”. After making it executable you can run it from your command prompt using “./filename”.

Related Articles


<< Go Back to Executing Bash Script | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment