How to Run a Script in Linux

Run Bash and executable scripts correctly using an interpreter, execute permissions and a shebang.

Linux gives you two common ways to run a script: pass it to its interpreter, or execute it directly with a valid shebang and execute permission.

Step by step

1 Run with Bash

$ bash script.sh

The file itself does not need execute permission when Bash reads it explicitly.

2 Make it executable

$ chmod +x script.sh

Adds execute permission without requiring world-write access.

3 Execute it directly

$ ./script.sh

The ./ prefix explicitly names the file in the current directory. A valid shebang should identify the interpreter.

4 Debug it

$ bash -x script.sh

Prints commands as Bash executes them.

Tips worth knowing

Frequently asked questions

Why do I need ./ before a script?

The current directory is normally not in PATH, so ./ explicitly names the local file.

Do I need chmod +x if I use bash script.sh?

No. Passing the file to Bash does not require the file itself to be executable.

Why do I get Permission denied?

The script may lack execute permission, a parent directory may block traversal, or the filesystem may use noexec.

If it goes wrong

Permission denied

The path resolves fine — the system simply will not let you do that with it.

The commands behind it

chmod

Change file permissions with numeric (755, 644) and symbolic (u+x) modes — clearly explained.

ls

List the contents of a directory, with options for hidden files, long format, sorting and more.

head & tail

Print the first or last lines of a file — including live log following with tail -f.

Concepts involved: File permissions

More how-to guides

Check disk space in Linux

See how much space is left, and find out what is using it.

Extract a .tar.gz archive

Unpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.

Free a port that is already in use

Find which process holds a port and stop it cleanly.

Find and replace text in files

Replace text in one file or across a whole project, safely.