Table of Contents
Why “making scripts executable” matters
Writing a script file (for example with nano myscript.sh) does not automatically mean you can run it like a normal command. On Linux, a file needs:
- The right permissions (the
x“execute” bit). - A proper shebang line so the system knows which interpreter to use.
This chapter focuses on those two practical steps and the different ways to execute scripts.
Adding a shebang (#!)
At the top of your script, you usually add a line like:
#!/bin/bashor the more portable:
#!/usr/bin/env bashCommon examples:
- Bash:
#!/bin/bashor#!/usr/bin/env bash - POSIX sh:
#!/bin/sh - Python:
#!/usr/bin/env python3 - Perl:
#!/usr/bin/env perl
If you omit the shebang:
- Running with an interpreter explicitly (e.g.
bash myscript.sh) will still work. - Running directly (e.g.
./myscript.sh) may fail or use a different default shell than you expect.
For this chapter, assume you’ve already added a sensible shebang to your script.
Making the script executable with chmod
To make a script file executable, you change its permissions using chmod.
Typical use: give yourself execute permission
If myscript.sh is in your home directory:
chmod u+x myscript.shu= user (the owner of the file)+x= add execute permission
You can now run it as:
./myscript.shSimple “everyone can execute” style
Sometimes tutorials use:
chmod +x myscript.shThis adds execute permission for:
- user (owner)
- group
- others
For personal scripts, this is usually fine, especially in your own home directory, but on shared systems be more cautious.
Running your script: three main ways
There are three common ways to run a script:
1. Explicit interpreter (does not need `+x`)
You can run a script even if it’s not executable:
bash myscript.sh
python3 myscript.pyThis ignores execute permissions and simply passes the file to the program.
Pros:
- Works even if the file isn’t executable.
- You control which interpreter is used.
Cons:
- Slightly more typing.
- If you move the script around or share it, the receiver must know the right interpreter.
2. Run as a program (needs `+x` and usually a shebang)
After chmod +x myscript.sh, run:
./myscript.shNotes:
./means “this directory” (otherwise the shell might not find it).- The system reads the shebang line to decide which interpreter to run.
Pros:
- Feels like running any other command.
- Shebang makes the script self-contained.
Cons:
- You must remember to set execute permissions.
- Shebang must be correct and executable itself.
3. Putting scripts in your PATH
If you store scripts in a directory that is part of your PATH (e.g. $HOME/bin or ~/.local/bin) and make them executable, you can run them by name without ./.
Example:
- Create a directory and add it to
PATH(one-time shell config; covered elsewhere). - Place script there, e.g.:
mv myscript.sh ~/.local/bin/myscript
chmod +x ~/.local/bin/myscript- Now just run:
myscript
No ./, no .sh needed if you don’t want it.
Checking if your script is executable
Use ls -l to see permission bits:
ls -l myscript.shExample before:
-rw-r--r-- 1 alice alice 1234 Dec 12 10:00 myscript.sh-rw-r--r--means: read/write for user, read for group/others, no execute.
After running chmod u+x myscript.sh:
-rwxr--r-- 1 alice alice 1234 Dec 12 10:01 myscript.shrwxfor the user now includesx(execute).
If you used chmod +x instead:
-rwxr-xr-x 1 alice alice 1234 Dec 12 10:01 myscript.sh- Everyone (user, group, others) has execute permission.
Common permission patterns for scripts
For scripts in your home directory:
- Only you can modify and execute:
chmod 700 myscript.sh
# equivalent to: chmod u+rwx,go-rwx myscript.sh- You can read and execute; others can read but not execute:
chmod 644 myscript.sh # not executable
chmod 755 myscript.sh # executable by everyoneQuick explanation of numeric modes (detailed permissions are discussed in the permissions chapter):
7=rwx5=r-x0=---
So 755 means rwxr-xr-x.
Running scripts from “unsafe” locations
Often you’ll download a script (e.g. install.sh) from the internet. Typical steps:
- Inspect it first:
less install.sh- Make it executable only if you trust it:
chmod +x install.sh- Run it explicitly with its path to avoid accidentally running something else:
./install.sh
You can also avoid chmod and run it with an interpreter:
bash install.shThis can be safer in some workflows, but the real safety comes from reviewing and understanding what the script does.
Executable but not in PATH: why `./` is necessary
Even if a file is executable, typing just its name (e.g. myscript.sh) won’t run it unless its directory is in your PATH.
- Shell searches directories listed in
$PATH. - Current directory (
.) is not usually inPATHfor security reasons.
So you must specify ./:
./myscript.sh
If you truly want to run it by name alone, put it in a directory that is in your PATH.
Shebang pitfalls and permissions
A few common issues when making scripts executable:
- No newline at the end of the shebang
Make sure the shebang is exactly the first line, followed by a newline:
#!/bin/bash
echo "Hello"- Wrong path in shebang
If your system’sbashis in/bin/bash,#!/usr/bin/bashwill fail. Use#!/usr/bin/env bashfor portability. - Interpreter not executable
The shebang’s target must itself be executable. If#!/usr/bin/envis broken or missing, the script will fail even if it is executable. - Permissions without read
For a script to run, you normally need both read and execute: -rwx------(700): OK--x------(100): usually fails because the interpreter can’t read the file.
Use chmod 700 or chmod 755, not just chmod +x on a file that had no read permission.
Quick “from scratch” workflow example
- Create a script:
nano hello.shContents:
#!/usr/bin/env bash
echo "Hello from my script!"- Save and exit.
- Make it executable:
chmod u+x hello.sh- Run it:
./hello.sh- Optionally, move into a directory on your
PATHand drop the.shextension:
mv hello.sh ~/.local/bin/hello
chmod u+x ~/.local/bin/hello
helloNow you’ve fully made the script executable and usable like a regular command.