← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 2 of 12

Running Scripts

Running a script is like handing a recipe to a cook: you can either say 'please follow this recipe' each time, or stick a note on it that says the cook may use it on their own.

In this page:

  1. Running Scripts
Syntax
bash
bash script_name.sh

chmod +x script_name.sh
./script_name.sh

Running Scripts

A Bash script is run either by passing it to the bash interpreter directly, or by making it executable and running it as its own program.

bash script.sh always works regardless of permissions. Running it directly as ./script.sh requires execute permission on the file first.

Note: bash script.sh works even without execute permission, since you're invoking bash explicitly.

Example: Running Scripts

bash
# Run with the interpreter directly
bash script.sh

# Or make it executable and run it directly
chmod +x script.sh
./script.sh

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Running hello.sh without ./, so the shell looks in PATH and reports command not found for a script in the current directory.
  2. Running ./hello.sh without execute permission, which gives Permission denied; use chmod +x or bash hello.sh.
  3. Running a script with sh script.sh and expecting Bash features, since sh may be a simpler shell.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.