How to Make Bash Script Executable Using chmod?

Bash scripts are widely used in Linux systems to automate various tasks. The script must be executable to get the result of the commands stored inside it. The “chmod” command line utility makes the script executable from the command line. 

This guide will demonstrate the “chmod” command’s basic syntax and practical implementation. 

Method 1: Using Alphabetical Representation of the chmod Command

The permissions of the “chmod” command can be represented using alphabetic order. The syntax to use this is as follows: 

Syntax:

The basic syntax of the “chmod” command is to make a Bash script executable below:

$ chmod a+x <script-name> or chmod a+x <script-name>     #for all users
$ chmod u+x <script-name>                                #for the current user

The alphabets “a” and “u” represents the all and current user respectively. At the same time, the “x” stands for execution rights. 

Using the +x (or a+x) Permission

To make the script executable for all users, use the “+x” or “a+x” flag as we did here: 

$ sudo chmod +x script.sh

This will add the execute permission (x) to the script for the user, group, and others.

Using the u+x Permission

The “u+x” option of the “chmod” command permits the current user only. For instance, specify the existing script file named “script.sh” in the following command:

$ sudo chmod u+x script.sh

This will add the execute permission (x) to the script for the user only.

Method 2: Using Octal Representation of the chmod Command

The octal numbers can represent the permissions in a chmod command, and the syntax for this is:

Syntax:

$ chmod YYY [script_name]

In the above, “YYY is a three-digit octal number representing the permissions for the owner, group, and others, respectively. Let’s understand the octal numbers associated with the permissions:

PermissionsReadWriteExecute
Octal Number421

Using the Octal Number 755

If the user wants that, only the owner can read, write, and execute the script file, while the group and others can only read and execute the script, execute the “chmod” command with the “755” permission:

$ sudo chmod 755 script.sh

The above execution sets 755 permissions, representing the owner will read, write, and execute the “script.sh” file, while the group and others will only read and execute.

Using the Octal Number 700

The permission “700” enables the owner only to read, write, and execute the script. For instance, specify the script file “script.sh” with the permission “700” as below:

$ sudo chmod 700 script.sh

This will set the permissions to 700, representing that the owner will only have authority to read, write, and execute the “script.sh” file, and others have no permission on the script

Using the Octal Number 744

To set the permission “744” to the script that represents the owner can read, write and execute the script, the group can read, and others have no permission on the script:

$ sudo chmod 744 script.sh

The output shows that permission “744” has been set to the “script.sh” file.

Verify the Execution

Users must run the script file with the prefix “./” to visualize whether the file is executable. To specify the local file named “script.sh” in the below command:

$ sudo ./script.sh

The above execution displays the output in the command line via the “chmod”. 

Conclusion

Linux offers the “chmod” command with the “x” permission to make a Bash script executable. Additionally, users can set the execute permissions using the octal numbers, i.e., “755”, “700”, and “744”. This article has explained the “chmod” command with different options to make a Bash script executable.