Save 10%Pay yearly on any plan. 6% half-yearly, 3% quarterly. Help centreWhatsAppTelegram TeamsOpen a ticket

How to Run a Shell Script in Linux and Ubuntu

Three ways to run a .sh file in Linux, what chmod +x and the first line do, how to pass arguments, and the fixes for "Permission denied" and "command not found".

3 min readBy Smita

Reviewed by the AmazingRDP technical team

Running a shell script in Linux

Scripts earn their keep on a server that is always on. A Linux VPS gives you root access. See Linux VPS from $6.99

A shell script is a text file with commands in it, run from top to bottom. Running one takes a single line, and there are three ways to write that line.

A script to try

nano hello.sh

Type this, then save with Ctrl + O, Enter, and leave with Ctrl + X:

#!/bin/bash
echo "Hello from $(hostname)"
echo "Today is $(date +%A)"

Way 1: hand it to bash

bash hello.sh

Works at once, with no preparation. Good for a quick run.

Way 2: make it executable and run it

chmod +x hello.sh
./hello.sh

chmod +x marks the file as a program; you do it once. The ./ means "in this folder". It is needed because Linux does not look in the current folder for commands, for safety.

Way 3: run it inside your current shell

source hello.sh

The first two ways start a new shell that ends with the script. source (or a single dot) runs the lines in your own shell, so variables it sets and folders it changes into remain afterwards. This is for settings files, not for ordinary scripts.

What the first line does

#!/bin/bash tells the system which program should read the file. With way 2 it decides the interpreter; with way 1 you have named it yourself. Use #!/bin/bash for scripts that use Bash features, and #!/usr/bin/env python3 style lines for other languages.

Pass arguments

#!/bin/bash
echo "First: $1"
echo "Second: $2"
echo "All: $@  (count: $#)"

Run it as ./args.sh apple "two words". Inside the script, $1, $2 are the arguments in order, $@ all of them and $# their number.

Run it from anywhere

Put the script in a folder that is in your PATH, and it becomes a command:

mkdir -p ~/.local/bin
mv hello.sh ~/.local/bin/hello
hello

On most systems ~/.local/bin is in the PATH after the next login. For all users, use /usr/local/bin with sudo.

Run as root, in the background, on a schedule

sudo ./backup.sh
nohup ./long-job.sh > job.log 2>&1 &

The second line keeps a script running after you log out and writes its output to a file. To run a script at fixed times, see how to schedule tasks with cron.

Errors and their causes

MessageCause and fix
Permission deniedThe file is not executable: chmod +x script.sh, or run it with bash script.sh
command not foundYou typed script.sh without ./
bad interpreter: No such file or directory, often with ^MThe file was saved on Windows with Windows line endings. Fix with sed -i 's/\r$//' script.sh or the tool dos2unix
Syntax error near unexpected tokenA typing mistake, or a Bash script run with sh. Use bash
It works by hand and not from cronCron has a short PATH. Use full paths inside the script

Find mistakes

bash -n script.sh
bash -x script.sh

The first checks the syntax without running anything; the second prints every command as it runs. Starting a script with set -euo pipefail makes it stop at the first error in place of carrying on.

Before you run someone else's script

Read it. A script does whatever its lines say, with your rights, and with root's rights if you add sudo. Never pipe a download straight into a shell unless you trust the source completely.

A Linux VPS is a good home for scripts that must run unattended. Related: fix "sudo: command not found" and learn Linux by playing games.

Questions

How do I run a .sh file in Linux?

Run bash file.sh, or make it executable with chmod +x file.sh and run ./file.sh.

Why do I get "Permission denied" when running a script?

The file is not marked executable. Run chmod +x on it, or start it with bash.

What does ./ mean before a script name?

It means "in the current folder". Linux does not search the current folder for commands unless you say so.

What is the #!/bin/bash line for?

It tells the system which program should run the script when you start it directly.