Make Shell Scripts Executable with chmod

Summary:
Understand file permissions and make your script runnable.


Shell scripts are powerful tools for automating tasks and streamlining workflows on Unix-like systems. But out of the box, a script file is often not ready for direct execution. Before you can run your shell script like any other program, you need to ensure it has the correct permissions. This post will walk you through the essentials of file permissions and how to make your shell scripts executable with the chmod command.


Understanding File Permissions

Every file in a Unix-like operating system has associated permissions that determine what users can do with it. These permissions are read (r), write (w), and execute (x), and they apply to three categories of users:

  • Owner: The user who owns the file.
  • Group: Other users belonging to the file’s group.
  • Others: Everyone else.

You can view a file’s permissions with the ls -l command. Here’s a typical output:

-rw-r--r-- 1 alice staff 2048 Jun 30 09:00 script.sh

Breaking down the first part:

  • -: File type (d = directory, - = regular file)
  • rw-: Owner permissions (read and write)
  • r--: Group permissions (read)
  • r--: Others permissions (read)

In this example, the script is not executable; there's no x in the permission strings.


Why Scripts Need Execute Permission

To run the script directly (e.g., ./script.sh), the file must have execute (x) permission. Without it, you'll see a "Permission denied" error:

$ ./script.sh
bash: ./script.sh: Permission denied

Making Scripts Executable with chmod

The chmod command (short for "change mode") modifies file permissions. Its most common syntax for adding execute permission is:

chmod +x script.sh

This command adds execute permission for all user categories (owner, group, others). After running this, check the permissions again:

-rwxr-xr-x 1 alice staff 2048 Jun 30 09:00 script.sh

Now the file is executable!

Setting More Restrictive Permissions

To make your script executable only by you (the owner), use:

chmod u+x script.sh
  • u+x: Adds execute permission for the user (owner).
  • g+x: Adds execute for the group.
  • o+x: Adds execute for others.

You can also specify exact permissions with numbers (octal notation). For example, to set the permission to rwxr--r-- (executable and writable by owner, readable by all):

chmod 744 script.sh

Running Your Script

Once the script is executable, you can run it directly:

./script.sh

Alternatively, you can always run the script with an explicit shell, regardless of permissions:

bash script.sh

But making it executable allows you to treat your script like any other command.


Recap

Enabling execute permission on your shell scripts is a fundamental step before running them directly. Just remember:

  • Use ls -l to check current permissions.
  • Use chmod +x script.sh to make it executable.
  • Run with ./script.sh.

Understanding file permissions is key not just for usability, but also for good security practices.

Happy scripting!