How to Make a Bash Script File Executable in Linux
A Bash script is essentially a text file containing a series of commands. To make a Bash script file executable means giving it permission to be run as a program.
In this blog post, we'll start by taking a deep dive into the concept of file permissions in Linux. We'll then learn how to use the chmod
command, exploring both its symbolic and octal notations, to make a Bash script file executable. Let’s get started!
Prerequisites
To follow along with the examples in this blog post, you'll need access to a Linux operating system.
For the purpose of this blog post, I'll be using Ubuntu as my Linux distribution of choice. Specifically, I will use KodeKloud’s Ubuntu playground, which lets you access a pre-installed Ubuntu operating system in just one click. Best of all, you won't need to go through the hassle of installing any additional software — everything you need is already set up and ready to use.
Note that the examples and commands in this blog post are meant to be executed in a Bash shell, which, by default, is one of the most common shell environments in Linux distributions.
How to Create a Script File
We'll begin by creating a simple Bash script file named demo.sh
. We’ll later turn it into an executable file.
Make sure you’re inside the directory where you want to save this file. Then, run the command below to create and open the demo.sh
file using the "nano" text editor:
nano demo.sh
Executing this command brings up the "nano" text editor right in your terminal, as shown below:
Next, add the following script to the "nano" editor:
#!/bin/bash
echo "Hello World!"
What you see is a basic "Hello World!" script. When executed, it will echo the message Hello, World!
to your terminal.
After adding this code to demo.sh
, it's time to save and exit the file. With "nano", you do this by pressing ctrl+o
to write the changes, followed by ctrl+x
to exit the editor.
Our script file is ready. In the next section, we’ll explore the concept of file permissions in Linux.
File Permissions in Linux
Linux uses three types of permission controls for files. They are:
- Read permission: Determines the ability to read the contents of a file.
- Write permission: Determines the ability to modify the contents of a file.
- Execute permission: Determines whether a user is allowed to execute (or "run") the file. If a file has execute permission, it means the system allows the file to be run as a program.
Note: In Linux, not all files store data. Some files can also store scripts or programs that perform specific tasks. For example, the demo.sh
file we created in the previous section doesn’t contain any data; it contains a script.
These permissions can be granted or revoked separately for three types of user classes:
- User: The owner of the file.
- Group: Users who are part of the file's group.
- Others: All other users on the system.
Now, how can we check the permissions of a given file?
Using the ls
command with the -l
option.
To check the permissions for the demo.sh
file, run the following command:
ls -l demo.sh
The output will look something like this:
The first column of the output (-rw-r--r--) is a string of ten characters that represents the file's permissions. Let’s understand what these characters mean.
The first character indicates the type of object. A -
(dash) means it's a regular file.
The next nine characters are divided into three sets, each comprising three characters. These represent the permissions given to the user (owner of the file), group (group to which the file belongs), and others (everyone else), respectively.
In each set of three characters:
- The first character is
r
if permission to read the file is given. - The second character is
w
if permission to write the file is given. - The third character is
x
if permission to execute the file is given. - If any of these permissions (read, write, execute) is not granted, it is represented by a dash (-).
For example, in the above output "-rw-r--r--":
- "-" indicates that "demo.sh" is a file.
- "rw-" indicates that the owner of the file can read and write the file but not execute it.
- "r--" indicates that the group members can only read the file but not modify or execute it.
- "r--" indicates that other users can only read the file but not modify or execute it.
Making a Bash Script File Executable in Linux
In the previous section, we learned that the demo.sh
file doesn’t have execute permission. This means we can’t run it as a program. Let’s confirm this.
Make sure you’re in the directory that contains the demo.sh
file. Then run the following command:
./demo.sh
You’ll see the message ./demo.sh: Permission denied
printed on your terminal like this:
Note: We can still run the demo.sh
file as a program without making it executable. We can do this by passing it as an argument to the bash
command. This is possible because the script file is in the root directory, which is in the system’s command path. But our goal is different. We want to run demo.sh
as a standalone program. To do this, the file must have execute permission.
So, how do we run the demo.sh
script file as a program?
The answer is: We have to make the demo.sh
file executable, meaning we need to grant it execute permission. We can do this using the chmod
(stands for "Change Mode") command, which has two modes of operation: symbolic mode and octal mode.
Using chmod command in symbolic mode
In symbolic mode, you specify permissions using letters instead of numbers. Here's how it works:
- You start by specifying who you want to change permissions for. This could be the user (owner) of the file, the group that owns the file, other users, or all users. You represent these with the letters
u
,g
,o
, anda
, respectively. - Next, you specify what change you want to make. This can be adding a permission, or removing a permission. You represent these with the symbols
+
and-
respectively. - Finally, you specify which permission you want to change. This can be the permission to read, write, or execute the file. You represent these with the letters
r
,w
, andx
, respectively.
Now that you understand how the chmod
symbolic mode works, let’s use it to add the execute permission to the demo.sh
file. Run the following command:
chmod u+x demo.sh
Here's what each part of the command means:
chmod
: This is the command to change permissions.u
: This specifies that you want to change permissions for the user (owner) of the file.+
: This specifies that you want to add permission.x
: This specifies that the permission you want to add is the execute permission.demo.sh
: This is the name of the file you want to change permissions for.
After running this, let’s verify that the execute permission has been applied. Run the following command:
ls -l demo.sh
After running this command, you’ll get an output like this:
As you can see, the file has now execute permission (for the owner of the file). Now, we can run it as a program with the following command (make sure you’re inside the directory that contains the demo.sh
file):
./demo.sh
After running this command, you’ll see the text Hello World!
printed to the terminal like this:
Using chmod command in octal mode
The second mode available in chmod
is called the octal mode. With octal mode, the permissions are represented as three numbers, one each for the owner, group, and other permissions.
The following table shows how the octal number matches the three symbolic mode permissions:
Our file demo.sh
currently has execute permission set. Let's remove this permission, so that we can re-add it again using the chmod
command’s octal notation.
Run the following command:
chmod u-x demo.sh
This command uses the symbolic notation u-x
to remove (-) the execute (x) permission for the user (u) from the demo.sh
file.
To confirm that the execute permission has been removed, run the following command:
ls -l demo.sh
As you can see in the output below, the owner no longer has execute permission.
Now, having removed the execute permission, let's re-add it, but this time using octal notation of the chmod
command. Run the following command:
chmod 744 demo.sh
This command tells the system to set the permissions of demo.sh
to 744, which in octal notation means the user (u) has read (4), write (2), and execute (1) permissions (adding up to 7), and the group (g) and others (o) have only read permissions (4).
To confirm that the changes have indeed been applied, let's inspect the file permissions. Run the ls -l
command as follows:
ls -l demo.sh
You should see output similar to this:
As indicated by the "-rwxr--r--", the demo.sh
file now has read, write, and execute permissions for the user. Let’s run it as a program as we did in the previous section with the following command (make sure you’re inside the directory that contains the demo.sh
file):
./demo.sh
You’ll see Hello World!
printed on the terminal, as shown below:
Congratulations! Now you know how to use the chmod
command in both symbolic and octal modes to make a Bash script file executable in Linux.
Conclusion
In this blog post, we understood the importance of file permissions in Linux and explored the meanings of read, write, and execute permissions and how they impact user interactions with files. Then, we demonstrated how to make a Bash script file executable using the chmod
command, exploring its two primary modes of operation: symbolic and octal notation.
So, which mode should you use, symbolic or octal? They both achieve the same end result - they control access to your files. The choice comes down to personal preference. That being said, I think the symbolic notation is a bit more intuitive for beginners. It's less abstract than the octal notation and can be easier to read at a glance.
Looking to build a solid foundation in Linux? Check out the following course from KodeKloud:
- Linux Basics Course: In this hands-on course, you'll master Linux basics. You'll learn how to use shells and package managers. You’ll also learn important Linux concepts such as networking, security, and storage. After each lecture, you'll get a chance to try out what you've learned with exercises you can do right in your web browser. This helps to make sure you really understand and remember what the course has covered. By the end of this course, you'll be able to confidently use a Linux CLI to navigate around a Linux operating system.