Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

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.
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#!/bin/bash runs the script with Bash. Use #!/usr/bin/env bash for a more portable path, or #!/bin/sh for the POSIX shell..sh suffix is a convention that signals a shell script; Linux does not strictly require it to execute the file.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.shls -l script.sh and confirm the permission string contains an x.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| Method | Command | Execute bit needed? | Uses shebang? |
|---|---|---|---|
| Current directory | ./script.sh | Yes | Yes |
| Explicit interpreter | bash script.sh | No | No |
| POSIX shell | sh script.sh | No | No |
| Full path / PATH | script.sh | Yes | Yes |
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.
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.shPass 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: 2Windows 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.
bash script.sh or ./script.sh.wsl ./script.sh..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.ps1On 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.shAlways 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.
chmod +x script.sh, or launch it with bash script.sh as a one-off.dos2unix script.sh or sed -i 's/\r$//' script.sh.PATH. Use ./script.sh or move the file into a PATH directory.dos2unix.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.
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.
./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.
"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.
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.
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.
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.
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