πŸ”§ 4. Bash Functions & Scripting
Estimated reading: 3 minutes 49 views

πŸš€ 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 bash command (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.sh and ./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 cron or 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

ErrorCauseFix
Permission deniedScript not executablechmod +x script.sh
command not found: script.shNot using ./ for local scriptsUse ./script.sh or full path
bad interpreter: No such fileInvalid or missing shebangUse #!/bin/bash as first line
Script not using Bash syntaxSystem default is /bin/shRun 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

MethodUses Shebang?Needs Execute PermissionNotes
bash script.sh❌ No❌ NoAlways runs under bash
./script.shβœ… Yesβœ… YesHonors 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.sh ignores shebang and doesn’t require execute permission
  • ./script.sh requires chmod +x and a valid shebang
  • Use ./ for production scripts with portability
  • Use bash for 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🟒 Bash: Running Scripts (bash script.sh, ./script.sh)

Or Copy Link

CONTENTS
Scroll to Top