CMD Master
Back to Blog
•Updated•Arnošt Havelka

How to Write and Run Your First Bash Script

Create a Bash script, check its syntax, make it executable, and understand the difference between bash script.sh and ./script.sh.

Start Interactive Lesson
How to Write and Run Your First Bash Script

A Bash script is a text file containing commands that Bash runs in order. To create your first one, save a shebang and a command in script.sh, check the file, grant execute permission, then launch it with ./script.sh. You can also use bash script.sh while diagnosing execution or permission problems. The first-script lesson walks through the same workflow in a prepared environment.

Save a shebang and one visible command

Create script.sh in a text editor with these two lines:

#!/usr/bin/env bash
printf '%s\n' 'Hello World'

The shebang tells the operating system to find bash through the current command search path when the file is launched directly. #!/bin/bash is also common when Bash is known to live at that exact path. Pick one deliberately rather than assuming every machine installs Bash in the same location.

Inspect the saved file:

cat script.sh

This catches an empty file, the wrong filename, or content saved in another directory. If the script came from Windows, make sure the editor saved Unix-style LF line endings. A hidden carriage return can produce a “bad interpreter” error even when the shebang looks correct on screen.

Check syntax before executing

Ask Bash to parse the file without running its commands:

bash -n script.sh

No output means Bash found no syntax error. It does not prove that paths exist, permissions are correct, or the commands will produce the intended result; it only checks parsing.

Make the script executable

Grant execute permission to the file's owner:

chmod u+x script.sh

Verify the permission:

ls -l script.sh

The owner's three-character permission group should now include x. chmod changes the file mode; it does not inspect or repair the script's contents.

Understand the two common ways to run it

Launch the executable file from the current directory with:

./script.sh

The ./ prefix supplies a path. Most shells do not search the current directory automatically, so entering only script.sh can report “command not found” even when the file exists.

Alternatively, ask Bash to read the file explicitly:

bash script.sh

This form needs a readable file but not its execute bit, and Bash handles the content regardless of the file's shebang. That makes it useful for separating “Bash cannot read or parse this file” from “the operating system cannot launch this executable file.”

Diagnose the message you actually receive

  • No such file or directory: use pwd and ls to confirm your location and filename.
  • Permission denied: inspect the execute bit, file owner, directory permissions, and mount options.
  • Bad interpreter: check the shebang path and line endings.
  • Syntax error: run bash -n script.sh, then inspect the reported line and nearby quotes or control words.
  • Unexpected output: run each command manually with the same inputs before adding more script logic.

Once this save-check-run loop is predictable, continue with Bash variables, functions, or the safer backup-script example. The GNU Bash shell-script reference explains interpreter selection and script invocation in more detail.

References

These documentation links provide authoritative details for the commands used in this article.

Up Next

How to Write a Safer Bash Backup Script with tar

Build a Bash backup script that validates its source, avoids silent overwrites, removes partial archives, and verifies the tar file.