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
- Use `./script.sh`, not `script.sh`, unless its directory is intentionally on PATH.
- Use LF line endings; CRLF can cause bad-interpreter errors.
- Avoid chmod 777.
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
The commands behind it
Change file permissions with numeric (755, 644) and symbolic (u+x) modes — clearly explained.
lsList the contents of a directory, with options for hidden files, long format, sorting and more.
head & tailPrint the first or last lines of a file — including live log following with tail -f.
Concepts involved: File permissions
More how-to guides
See how much space is left, and find out what is using it.
Extract a .tar.gz archiveUnpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.
Free a port that is already in useFind which process holds a port and stop it cleanly.
Find and replace text in filesReplace text in one file or across a whole project, safely.