How to run .sh file in Linux: bash script.sh (no execute permission needed) or chmod +x script.sh && ./script.sh (uses the shebang line). The shebang #!/bin/bash at the top tells the kernel which interpreter to use. The team that uses chmod +x + ./script.sh has the standard execution pattern.
Table of contents
- Method 1: bash script.sh
- Method 2: chmod + execute
- The shebang line
- Why ./script.sh not script.sh
- PATH for executables anywhere
- Debugging with bash -x
- Common pitfalls
- FAQ
Method 1: bash script.sh
bash script.sh
No execute permission needed. The team that uses this for one-off scripts has the simplest method.
Method 2: chmod + execute
chmod +x script.sh
./script.sh
The team that uses chmod +x then ./script.sh has the standard execution. Requires the shebang line at the top of the script.
The shebang line
First line of script.sh:
#!/bin/bash
Other common shebangs:
#!/bin/sh- POSIX shell (most portable).#!/usr/bin/env bash- finds bash in PATH (more portable).#!/usr/bin/env python3- for Python scripts.#!/usr/bin/env node- for Node scripts.
The team that uses #!/usr/bin/env has portable scripts (no hardcoded path).
Why ./script.sh not script.sh
Linux doesn’t run scripts in the current directory by default (unlike Windows). The ./ prefix is needed. The team that types script.sh and gets ‘command not found’ needs the ./.
PATH for executables anywhere
For scripts to be runnable from anywhere:
mkdir -p ~/.local/bin
mv script.sh ~/.local/bin/mycommand
chmod +x ~/.local/bin/mycommand
Add ~/.local/bin to PATH (often default). The team that uses ~/.local/bin has user-installed commands runnable from anywhere.
Debugging with bash -x
bash -x script.sh
Prints each command before execution. The team that uses bash -x has full debug output for failing scripts.
Common pitfalls
- Wrong shebang:
#!/bin/bashbut script uses sh-only syntax. The team that matches shebang to script has correct behavior. - CRLF line endings (from Windows): bash chokes.
dos2unix script.sh. - No execute permission:
chmod +xfirst. - Wrong working directory: use absolute paths or
cdin script.
FAQ
Why use ./script.sh?
Linux doesn’t include the current directory in PATH (security). The team that uses ./ has explicit intent.
Can I run a script without execute permission?
Yes - bash script.sh or sh script.sh. The team that uses this avoids chmod.
What’s a shebang?
The first line of a script starting with #! that tells the kernel which interpreter to use. The team that uses shebangs has portable scripts.
How do I run a script in the background?
nohup ./script.sh & or with systemd: create a service unit. The team that has systemd manages scripts as services.
Can I make a script run at boot?
Yes - via systemd service, cron @reboot, or /etc/rc.local. The team that uses systemd has proper service management.
If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.
Useful related references: