How to Write and Run a Bash Script in Linux

Tested on RHEL 10.2 (Coughlan)
Package bash 5.2.26-6.el10
coreutils 9.5-8.el10_2
nano 8.1-5.el10
Applies to Linux distributions with Bash installed
Privilege Normal user for all examples
Scope Covers how to create your first Bash script, add a shebang, run it with bash or ./script.sh, set execute permission, and fix common beginner errors. Variables, arguments, conditionals, loops, and functions are outside the scope of this article.
Related guides Shell scripting tutorial
Short on time?Beta Get the gist of this 7-minute read in seconds, then ask follow-up questions

A Bash script is a text file containing commands that Bash executes in sequence. To create one, write commands in a plain-text file. You can run it by passing the file to Bash with bash script.sh, or add a #!/bin/bash shebang, make the file executable, and run it as ./script.sh.

This guide shows both ways to run a Bash script and fixes the most common beginner errors: permission denied, command not found, bad interpreter / CRLF line endings, and errors caused by running Bash-specific syntax with sh.


Quick Answer: Create and Run a Bash Script

Open a new file in Nano:

bash
nano hello.sh

Type these lines:

text
#!/bin/bash
echo "Hello, World!"

Press Ctrl+O, then Enter, then Ctrl+X to save and exit.

Run the file without execute permission:

bash
bash hello.sh
output
Hello, World!

Add execute permission and run it by path:

bash
chmod +x hello.sh
./hello.sh
output
Hello, World!

What Is a Bash Script?

Four terms appear together in tutorials. They are related, but not interchangeable.

  • Terminal — the window or session where you type commands and read output (see Linux command line basics if that distinction is new).
  • Shell — the program that reads your input, runs commands, and expands text before execution.
  • Bash — a common shell with extensions such as [[ … ]] and arrays. Scripts in this guide target Bash.
  • Shell scripting — saving shell commands in a text file and running that file as a program.

A Bash script is a file of shell commands that Bash reads and executes non-interactively, in order from top to bottom. The .sh suffix is a naming convention only. Linux does not require it.


Write Your First Bash Script

Open a new file in Nano:

bash
nano hello.sh

You can use Vim, VS Code, or any plain-text editor instead. Nano is used here because it is simple to use from a terminal.

Type these two lines in the editor:

text
#!/bin/bash
echo "Hello, World!"

The first line is the shebang. It tells the kernel to run /bin/bash when you execute the file as ./hello.sh. The second line is a normal shell command. Bash runs it just as it would if you typed the command in the terminal.

Save and exit Nano: press Ctrl+O, then Enter to confirm the filename, then Ctrl+X to leave the editor.

#!/bin/bash is used throughout this beginner example. Other shebang forms, including #!/usr/bin/env bash, have different portability trade-offs. See the Bash shebang guide for #!/usr/bin/env bash and other interpreter options.


Run the Script with bash

Run the file by passing it to Bash explicitly:

bash
bash hello.sh
output
Hello, World!

bash hello.sh starts a separate Bash process that reads your file. Execute permission is not required.

When you run bash hello.sh, you explicitly choose Bash as the interpreter. The shebang is not used to select the interpreter in this case. If the first line were #!/bin/dash, bash hello.sh would still run the body with Bash.

Use bash hello.sh when you want Bash to interpret the file or when the script does not have execute permission.


Make the Script Executable and Run ./script.sh

To run the script as ./hello.sh, the file needs execute permission. Add it with Linux file permissions and chmod +x:

bash
chmod +x hello.sh
output
(no output on success)

Run it from the current directory with ./:

bash
./hello.sh
output
Hello, World!

The ./ prefix matters. The current directory (.) is usually not included in your PATH. If you type only hello.sh, Bash searches the directories in PATH instead of automatically looking in the current directory.

Reproduce the missing-prefix error:

bash
hello.sh
output
bash: hello.sh: command not found

./hello.sh explicitly refers to the hello.sh file in the current directory.

Arguments such as ./script.sh file.txt are covered in the Bash script arguments guide.


bash vs ./ vs sh vs source

The same file can be started four different ways. The main differences are whether execute permission is required, which shell interprets the file, whether the shebang is used, and whether your current shell session is affected.

Command Execute permission Interpreter Shebang Current shell affected?
bash script.sh No Bash No No
./script.sh Yes Interpreter specified by the shebang Yes No
sh script.sh No sh No No
source script.sh or . script.sh No Current shell No Yes

source script.sh reads and executes the file in your current shell instead of starting a separate shell. It is commonly used for shell configuration files or scripts that need to change the current shell's variables or working directory. It is not the usual way to run a standalone script.

bash script.sh explicitly runs the file with Bash, so the shebang does not select the interpreter. With ./script.sh, the shebang determines which interpreter runs the file. sh script.sh explicitly invokes the sh command available on that system, which may not support Bash-specific syntax.


A Useful Bash Script Example

Save the following as sysinfo.sh to print basic system information:

text
#!/bin/bash

echo "System information"
hostname
whoami
date
df -h /

Mark it executable and run it:

bash
chmod +x sysinfo.sh
./sysinfo.sh
output
System information
vm1.lab.example
root
Mon Sep  7 02:07:04 PM IST 2026
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   25G   15G   11G  58% /

These are ordinary Linux commands that you could also run individually in the terminal. The script simply runs them one after another.


Common Bash Script Errors

Permission denied

You ran ./script.sh but the file is not executable. Remove execute permission and try again:

bash
chmod -x hello.sh
./hello.sh
output
bash: ./hello.sh: Permission denied

Restore execute permission and rerun:

bash
chmod +x hello.sh
./hello.sh
output
Hello, World!

Or call Bash explicitly: bash hello.sh.

script.sh: command not found

You omitted ./ and the script directory is not in PATH:

bash
hello.sh
output
bash: hello.sh: command not found

Fix: run ./hello.sh from the script's directory, or use bash /full/path/hello.sh.

Bad interpreter or /bin/bash^M error (CRLF line endings)

The file was edited on Windows and contains carriage return characters (\r) at line ends. Create a script with Windows-style CRLF line endings and run it:

bash
printf '#!/bin/bash\r\necho hi\r\n' > crlf.sh
chmod +x crlf.sh
./crlf.sh
output
bash: ./crlf.sh: cannot execute: required file not found

On some systems the message is /bin/bash^M: bad interpreter instead. Both messages indicate the same CRLF line-ending problem.

Save with Unix line endings (LF only). On the host you can convert an existing file:

bash
sed -i 's/\r$//' crlf.sh
chmod +x crlf.sh
./crlf.sh
output
hi

Bash syntax fails with sh

On Debian and Ubuntu, /bin/sh commonly points to dash. A Bash-specific script may fail when you run sh script.sh instead of bash script.sh.

Create bash-only.sh with these contents:

text
#!/bin/bash
[[ -f /etc/passwd ]] && echo "File exists"

On Debian or Ubuntu, running the script with sh can produce:

bash
sh bash-only.sh
output
bash-only.sh: 2: [[: not found

On RHEL-family systems, /bin/sh commonly resolves to Bash, so you may not see the same error there. sh script.sh explicitly asks sh to interpret the file, so the #!/bin/bash shebang does not override that choice.

Run the file with Bash when the script uses Bash syntax:

bash
bash bash-only.sh
output
File exists

Fix: use bash script.sh, use #!/bin/bash with ./script.sh, or rewrite for POSIX sh if portability requires it.


Summary

  • A Bash script is a text file containing commands that Bash runs in order.
  • Run it without execute permission using bash script.sh.
  • To run ./script.sh, the file needs execute permission and a valid shebang.
  • bash, ./, sh, and source are not interchangeable.

References


Frequently Asked Questions

Does a Bash script need a .sh extension?

No. The .sh suffix is only a naming convention. When a script is executed directly as ./script, its execute permission and shebang matter; when you run bash script, Bash reads the file regardless of its filename extension.

Why do I need ./ before the script name?

The shell searches PATH for command names. The current directory is usually not in PATH, so ./hello.sh means “run this file in the directory I am in.”

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

bash script.sh runs the file with the Bash binary you typed; execute permission is optional and the shebang is ignored. ./script.sh executes the file directly, so the file needs execute permission and a valid shebang determines which interpreter runs it.

Can I run a Bash script with sh?

You can run sh script.sh, but the file will be interpreted by the system's sh, which is not guaranteed to be Bash. Use bash script.sh or #!/bin/bash with ./script.sh when the script uses Bash syntax.

Finished reading?Beta Get the key takeaways in seconds, then ask follow-up questions
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with 15+ years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)