Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How to Run a Shell Script?

To run a shell script, save your commands in a file with a .sh extension and a shebang line such as #!/bin/bash, make the file executable with chmod +x script.sh, and then run it with ./script.sh. If you would rather skip the execute permission, hand the file straight to an interpreter with bash script.sh or sh script.sh. The same script can take arguments, run from any directory on your PATH, be scheduled with cron, and run on Windows through Git Bash or WSL.

This guide covers the execution mechanics end to end. If you instead want to understand the bigger picture, see What Is Shell Scripting Used for? before you dive into running one.

Step 1 - Create a Shell Script

Create a plain-text file in any editor (nano, vim, or VS Code) and save it with a .sh extension. The first line should be a shebang that tells the system which interpreter to use.

#!/bin/bash
echo "Hello from my shell script"
date
  • Shebang line: #!/bin/bash runs the script with Bash. Use #!/usr/bin/env bash for a more portable path, or #!/bin/sh for the POSIX shell.
  • File extension: the .sh suffix is a convention that signals a shell script; Linux does not strictly require it to execute the file.
  • Commands: add any commands you would type in the terminal, one per line, below the shebang.

Step 2 - Make the Script Executable

Newly created scripts do not have execute permission by default, so the operating system will refuse to run them directly. Add the execute bit with chmod.

chmod +x script.sh
  • chmod +x: grants execute permission to the owner, group, and others.
  • chmod u+x: grants execute permission only to the file owner, which is the safer choice on shared machines.
  • Verify it: run ls -l script.sh and confirm the permission string contains an x.

Step 3 - Run the Shell Script (Linux and macOS)

Once the file is executable, you can run it from the directory that contains it. There are several ways to launch a script, and which one you pick affects whether the execute bit and the shebang matter.

# Run from the current directory (needs chmod +x)
./script.sh

# Run through Bash explicitly (no execute bit needed)
bash script.sh

# Run through the POSIX shell
sh script.sh

# Run using the full absolute path
/home/user/scripts/script.sh
MethodCommandExecute bit needed?Uses shebang?
Current directory./script.shYesYes
Explicit interpreterbash script.shNoNo
POSIX shellsh script.shNoNo
Full path / PATHscript.shYesYes

The ./ prefix is required when you run from the current directory because Linux only searches the folders listed in $PATH for executables, and your working directory is not on that list by default.

Run a Shell Script From Anywhere on the PATH

If you want to run a script by its bare name from any directory, place it in a folder that is on your PATH, such as ~/bin or /usr/local/bin.

# Move the script into a PATH directory
mkdir -p ~/bin
mv script.sh ~/bin/

# Make sure ~/bin is on the PATH (add to ~/.bashrc to persist)
export PATH="$PATH:$HOME/bin"

# Now run it by name from any directory
script.sh

Pass Arguments to a Shell Script

Pass values after the script name and read them inside the script with positional parameters. This makes a single script reusable across different inputs.

#!/bin/bash
echo "First argument: $1"
echo "Second argument: $2"
echo "All arguments: $@"
echo "Number of arguments: $#"

Run it with two arguments and the parameters fill in automatically:

./script.sh hello world
# First argument: hello
# Second argument: world
# All arguments: hello world
# Number of arguments: 2

Run a Shell Script on Windows

Windows does not ship with a native Bash interpreter, so a .sh file will not run from the Command Prompt. Use one of these options instead.

  • Git Bash: installed with Git for Windows. Right-click the folder, choose "Git Bash Here", then run bash script.sh or ./script.sh.
  • WSL (recommended): the Windows Subsystem for Linux gives you a real Linux shell. Run the script the same way you would on Linux, or call it from PowerShell with wsl ./script.sh.
  • PowerShell .ps1: a PowerShell script is a different language from a Bash .sh file and cannot run Bash syntax. Run a PowerShell script with the command below.
# Run a Bash script from PowerShell via WSL
wsl ./script.sh

# Run a native PowerShell script (.ps1 is not Bash)
powershell -ExecutionPolicy Bypass -File .\script.ps1

Schedule a Shell Script With Cron

On Linux and macOS, cron runs scripts on a recurring schedule without any manual trigger. Edit your crontab and add a schedule expression followed by the absolute path to the script.

# Open the crontab editor
crontab -e

# Run the script every day at 2:30 AM
30 2 * * * /home/user/scripts/script.sh

# Run the script once at every system boot
@reboot /home/user/scripts/script.sh

Always use absolute paths inside cron jobs. Cron runs with a minimal environment and a short PATH, so relative paths and assumptions about your shell profile will often fail silently.

Common Errors and Fixes

  • Permission denied: the file has no execute bit. Run chmod +x script.sh, or launch it with bash script.sh as a one-off.
  • bad interpreter: /bin/bash^M: the script has Windows-style CRLF line endings. Convert them with dos2unix script.sh or sed -i 's/\r$//' script.sh.
  • command not found: you typed the bare name but the directory is not on your PATH. Use ./script.sh or move the file into a PATH directory.
  • No such file or directory (on a file that exists): the shebang points to a missing interpreter or the file has CRLF endings. Fix the shebang path and run dos2unix.
  • Syntax error near unexpected token: you ran Bash-specific syntax with sh script.sh. Run it with bash instead of sh.

These same execution steps power test and build scripts in CI/CD. Teams often run their shell-driven automation against a cloud grid like so the same .sh entry point can launch cross-browser and real-device tests across many environments in parallel.

Frequently Asked Questions

How do I run a .sh file in Linux?

Open a terminal in the folder that contains the file, make it executable with chmod +x script.sh, then run it with ./script.sh. If you do not want to set the execute bit, run it directly through the interpreter with bash script.sh or sh script.sh.

What is the difference between ./script.sh, bash script.sh, and sh script.sh?

./script.sh executes the file directly, so it needs the execute permission set and a valid shebang. bash script.sh and sh script.sh hand the file to a named interpreter, only need read permission, and ignore the shebang. Use bash for Bash-specific syntax; sh runs in the more limited POSIX shell.

Why do I get "Permission denied" when running my shell script?

"Permission denied" almost always means the file does not have the execute bit set. Run chmod +x script.sh and try ./script.sh again. As a one-off workaround, you can also run it with bash script.sh, which does not require the execute permission.

How do I run a shell script on Windows?

Windows has no native Bash, so use Git Bash or the Windows Subsystem for Linux (WSL). In Git Bash, run bash script.sh or ./script.sh; in WSL, open the Linux shell and run it just as you would on Linux. PowerShell .ps1 files are a different language and are run with powershell -File .\script.ps1.

How do I pass arguments to a shell script?

Pass values after the script name, for example ./script.sh hello world. Inside the script, they are available as positional parameters: $1 is the first argument, $2 the second, $@ is all of them, and $# is the count.

How do I run a shell script automatically on a schedule?

Use cron on Linux and macOS. Run crontab -e and add a line such as 30 2 * * * /home/user/script.sh to run it every day at 2:30 AM. Always use absolute paths inside cron jobs because cron runs with a minimal environment. To run a script at boot, use the @reboot keyword or a systemd service.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests