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

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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

  1. At first, open your Ubuntu Terminal application.

  2. Next, write the following command to add permission to your script.

    chmod u+x test.sh

    Add executive permission

  3. Finally, press the ENTER button.

  4. 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!”.

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:

  1. First, open your File Manager, which is commonly found in the applications menu or the system toolbar.Open file manager

  2. Locate the Bash file & right-click on it to open the context menu. Select the “Properties” option from there.Open bash file properties
  3. 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
  4. 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.

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