π Bash: Running Scripts β Using bash script.sh and ./script.sh Explained
π§² Introduction to Running Bash Scripts β Execute with bash or ./
Once a Bash script is written, you need to execute it. There are two primary ways to run a Bash script:
- Using the
bashcommand (explicit interpreter) - Using
./script.sh(execute permission + shebang)
Each method has different implications on permissions, execution environment, and portability. Understanding them helps ensure your scripts run reliably across systems.
π― In this article, youβll learn:
- How to execute scripts using
bash script.shand./script.sh - The difference between interpreting vs executing directly
- Why execution permissions and shebang matter
- Best practices for safe and consistent script execution
π§ͺ Method 1: Run with bash script.sh
This method explicitly calls the Bash interpreter:
bash myscript.sh
β Use this when:
- The script is not executable
- You want to override the shebang interpreter
- You’re testing with different shell versions
π This method ignores the shebang (
#!/bin/bash) line and uses the specified interpreter (bash) directly.
π§ͺ Method 2: Run with ./script.sh
This method requires the script to be executable and relies on the shebang line.
Steps:
chmod +x script.sh # Make script executable
./script.sh # Execute script directly
β Use this when:
- You want the OS to auto-detect the interpreter from the shebang
- The script should be portable and standalone
- You’re scheduling it in
cronor calling from another program
π‘ If the script lacks a valid shebang, it may fail or be run by the wrong shell (
/bin/sh).
β οΈ Common Errors and Fixes
| Error | Cause | Fix |
|---|---|---|
Permission denied | Script not executable | chmod +x script.sh |
command not found: script.sh | Not using ./ for local scripts | Use ./script.sh or full path |
bad interpreter: No such file | Invalid or missing shebang | Use #!/bin/bash as first line |
| Script not using Bash syntax | System default is /bin/sh | Run with bash script.sh or add shebang |
π Example Script
#!/bin/bash
echo "Hello from Bash script!"
Save this as hello.sh, then:
chmod +x hello.sh
./hello.sh
β Output:
Hello from Bash script!
π§ Summary of Execution Differences
| Method | Uses Shebang? | Needs Execute Permission | Notes |
|---|---|---|---|
bash script.sh | β No | β No | Always runs under bash |
./script.sh | β Yes | β Yes | Honors the shebang interpreter |
π Summary β Running Bash Scripts
You can run Bash scripts using either bash script.sh (interpreter-first) or ./script.sh (shebang-based execution). Knowing when to use each method helps you create robust and portable scripts for real-world tasks.
π Key Takeaways:
bash script.shignores shebang and doesn’t require execute permission./script.shrequireschmod +xand a valid shebang- Use
./for production scripts with portability - Use
bashfor testing or interpreter-specific runs
βοΈ Real-world Uses:
- Deploying system automation via executable scripts
- Running scripts from cron or CI/CD pipelines
- Debugging with specific shell interpreters
β FAQ β Running Bash Scripts
β Do I need #!/bin/bash in every script?
β
Yes, if you use ./script.sh, it tells the system which interpreter to use.
β Why does ./script.sh give βpermission deniedβ?
β
You havenβt made it executable. Run:
chmod +x script.sh
β Is bash script.sh better than ./script.sh?
β
It depends. bash script.sh is great for manual testing, while ./script.sh is preferred for production usage and portability.
β Can I run a script without shebang?
β
Yes, but only with bash script.sh. ./script.sh requires a valid shebang to work correctly.
β How do I run a script from any directory?
β
Place it in a directory listed in $PATH, or call it with full path:
/path/to/script.sh
Share Now :
