Table of Contents
Understanding Executable Scripts
When you write a shell script, it is just a text file until the system knows it can be executed. Making a script executable tells Linux that the file is a program that can be run directly, instead of only being read or passed manually to a shell.
Linux decides if a file can be executed by looking at its permission bits and sometimes its content. In this chapter you focus on how to set those permissions and how to run your scripts correctly.
A script is not automatically executable just because it contains valid shell commands or starts with a shebang. You must set execute permissions or explicitly run it with a shell.
The Execute Permission Bit
Linux uses three basic permission types: read (r), write (w), and execute (x). They can apply to three classes: owner, group, and others. Here you only need to focus on the execute permission for scripts.
A permission string for a typical script might look like this when listed with ls -l:
-rwxr-xr-x 1 alice alice 1234 Jan 7 12:00 hello.sh
Here the x letters show that the file is executable. Without them, it might look like this:
-rw-r--r-- 1 alice alice 1234 Jan 7 12:00 hello.shThe second example means the file is readable and writable, but not executable by anyone.
Checking Script Permissions
Before making a script executable, it is useful to check its current permissions. Use ls -l followed by the filename:
ls -l hello.shYou will see a line that begins with 10 characters, for example:
-rw-r--r--
The important part here is whether there is an x for the owner, group, or others. If there is no x, you cannot run the script directly.
Making a Script Executable with chmod
The standard tool for changing permissions is chmod. To make your own script executable for you as the owner, you usually add the execute bit to the owner permissions.
To add execute permission for the owner only, use:
chmod u+x hello.sh
After this command, ls -l hello.sh might show:
-rwxr--r-- 1 alice alice 1234 Jan 7 12:00 hello.sh
Here u means the user (owner), + means add, and x means execute.
If you want the script to be executable for everyone, you can add execute permission to all classes:
chmod a+x hello.sh
This sets execute for user, group, and others. Now ls -l might show:
-rwxr-xr-x 1 alice alice 1234 Jan 7 12:00 hello.sh
You can also use numeric modes. A common pattern for scripts is 755, which means read, write, execute for the owner and read, execute for group and others:
chmod 755 hello.sh
This gives the same result as chmod u=rwx,go=rx hello.sh.
Be careful with chmod 777. It gives read, write, and execute permissions to everyone. On multiuser systems or scripts that handle sensitive data, this is unsafe.
The Shebang and Direct Execution
Once your script has the execute bit set, Linux will try to run it using the interpreter specified in the first line of the file, called the shebang. It usually looks like:
#!/bin/bashor a more portable form:
#!/usr/bin/env bash
When you run the script directly, the kernel reads this first line and starts the specified interpreter, passing your script as input. If there is no valid shebang, Linux may refuse to run the script directly or may attempt to use /bin/sh depending on the system configuration.
Setting execute permission and having a correct shebang work together. With both in place, you can run the script as a normal command.
Running Executable Scripts from the Current Directory
Assume you have a script named hello.sh in your current directory, and you have given it execute permissions, for example:
chmod u+x hello.shIf you try to run it by simply typing:
hello.shyou will often see an error like:
bash: hello.sh: command not found
This happens because the current directory is usually not in the PATH environment variable for safety reasons. To run a script in the current directory, you must include the relative path:
./hello.sh
Here . means "this directory" and ./hello.sh tells the shell to run the hello.sh file from the current directory. If the script is executable and has a valid shebang, it should now run.
Do not add the current directory (.) to your PATH as a permanent solution. It makes it easier to accidentally run malicious scripts mistakenly placed in your working directory.
Executing a Script Without Changing Permissions
Sometimes you may want to run a script that is not executable or that is not even yours to modify. In that case you can run it by explicitly invoking the interpreter, for example:
bash hello.shor:
sh hello.sh
Here bash or sh reads the script and executes it. This does not require the x permission on the file. It only requires read permission. This method is useful if you are experimenting with a new script and do not want to adjust its permissions yet.
However, for scripts that you will use regularly, giving them the execute bit and running them with ./scriptname is more convenient and is the normal practice.
Making Scripts Accessible from Anywhere
To run your scripts from any directory without typing ./, you can place them in a directory that is already in your PATH, or you can add a dedicated scripts directory to your PATH.
First, check your PATH:
echo "$PATH"You will see a list of directories separated by colons, for example:
/home/alice/.local/bin:/usr/local/bin:/usr/bin:/bin
If you create a directory such as ~/bin and ensure it is in your PATH, any executable script you place there can be run by typing its name alone.
Create the directory:
mkdir -p ~/binMove your script there and make it executable:
mv hello.sh ~/bin/
chmod u+x ~/bin/hello.sh
Then make sure ~/bin is in your PATH. How you do this permanently belongs to shell configuration, which is covered elsewhere. For now, you can adjust PATH for the current session:
export PATH="$HOME/bin:$PATH"After this, you can run your script from anywhere just by typing:
hello.sh
The shell will search through the directories in PATH, find the executable file named hello.sh, and run it with the interpreter specified in its shebang.
Ownership and Executable Scripts
You can only change the permissions of a file that you own or for which you have the right privileges, usually through sudo. If you try to run chmod on a file owned by another user without sufficient rights, you will get a "Operation not permitted" error.
For scripts that you install systemwide, you may need administrative privileges to change permissions. In that case you might run:
sudo chmod 755 /usr/local/bin/myscriptThis is typical for scripts that act like regular system commands.
Summary
To make a script executable, you add the execute bit with chmod, usually with chmod u+x for your own scripts or chmod 755 for scripts meant to be shared. You then run the script with a path, typically ./scriptname from the current directory or just scriptname if it resides in a directory listed in PATH. The shebang at the top of the script determines which interpreter is used when the script is executed directly.