Kahibaro
Discord Login Register

Making scripts executable

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:

  1. The right permissions (the x “execute” bit).
  2. 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/bash

or the more portable:

#!/usr/bin/env bash

Common examples:

If you omit the shebang:

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.sh

You can now run it as:

./myscript.sh

Simple “everyone can execute” style

Sometimes tutorials use:

chmod +x myscript.sh

This adds execute permission for:

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.py

This ignores execute permissions and simply passes the file to the program.

Pros:

Cons:

2. Run as a program (needs `+x` and usually a shebang)

After chmod +x myscript.sh, run:

./myscript.sh

Notes:

Pros:

Cons:

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:

  1. Create a directory and add it to PATH (one-time shell config; covered elsewhere).
  2. Place script there, e.g.:
   mv myscript.sh ~/.local/bin/myscript
   chmod +x ~/.local/bin/myscript
  1. 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.sh

Example before:

-rw-r--r-- 1 alice alice 1234 Dec 12 10:00 myscript.sh

After running chmod u+x myscript.sh:

-rwxr--r-- 1 alice alice 1234 Dec 12 10:01 myscript.sh

If you used chmod +x instead:

-rwxr-xr-x 1 alice alice 1234 Dec 12 10:01 myscript.sh

Common permission patterns for scripts

For scripts in your home directory:

  chmod 700 myscript.sh
  # equivalent to: chmod u+rwx,go-rwx myscript.sh
  chmod 644 myscript.sh   # not executable
  chmod 755 myscript.sh   # executable by everyone

Quick explanation of numeric modes (detailed permissions are discussed in the permissions chapter):

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:

  1. Inspect it first:
   less install.sh
  1. Make it executable only if you trust it:
   chmod +x install.sh
  1. 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.sh

This 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.

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:

  1. 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"
  1. Wrong path in shebang
    If your system’s bash is in /bin/bash, #!/usr/bin/bash will fail. Use #!/usr/bin/env bash for portability.
  2. Interpreter not executable
    The shebang’s target must itself be executable. If #!/usr/bin/env is broken or missing, the script will fail even if it is executable.
  3. 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

  1. Create a script:
   nano hello.sh

Contents:

   #!/usr/bin/env bash
   echo "Hello from my script!"
  1. Save and exit.
  2. Make it executable:
   chmod u+x hello.sh
  1. Run it:
   ./hello.sh
  1. Optionally, move into a directory on your PATH and drop the .sh extension:
   mv hello.sh ~/.local/bin/hello
   chmod u+x ~/.local/bin/hello
   hello

Now you’ve fully made the script executable and usable like a regular command.

Views: 24

Comments

Please login to add a comment.

Don't have an account? Register now!