Checking several paths one at a time gets tedious, especially when a filename contains spaces. This tutorial on Bash scripting builds a small command that accepts paths, checks each one, and reports whether it is a regular file.
The Bash script example in this tutorial also returns a status suitable for another command to inspect. You will see how arguments, variables, quotes, conditions, and loops work together in one practical task.
Bash Script Tutorial: Create the file checker
Create a file named check-files.sh and add this code:
#!/usr/bin/env bash
if [ "$#" -eq 0 ]; then
printf 'Usage: %s FILE...\n' "$0" >&2
exit 2
fi
missing=0
for path in "$@"; do
if [ -f "$path" ]; then
printf 'OK: %s\n' "$path"
else
printf 'Not a regular file: %s\n' "$path" >&2
missing=1
fi
done
exit "$missing"
Run the Bash script tutorial example from its directory. The first command gives the file execute permission; the second passes two paths as arguments:
chmod +x check-files.sh
./check-files.sh "report final.txt" notes.txt
The first line, called a shebang, asks the system to find Bash through env when the file is executed directly. The ./ prefix tells the shell to run the file in the current directory. You can also run bash check-files.sh "report final.txt" notes.txt without making it executable.
Follow the arguments
This bash scripting tutorial uses $# for the number of arguments and $0 for the script’s name. If there are no paths, [ "$#" -eq 0 ] succeeds, the usage message goes to standard error through >&2, and exit 2 ends the script. The square brackets are a command, so spaces around their contents matter.
The variable missing starts at zero. The for loop assigns each argument to path in turn. "$@" preserves each argument as a separate item, including a filename with spaces. Quoting "$path" likewise keeps that filename together when the test and printf receive it.
The condition [ -f "$path" ] succeeds for a regular file, including a symbolic link that resolves to one. A directory, a missing path, or a broken link goes to the else branch. That branch sets missing=1, and later successes do not reset it. This scripting tutorial for Bash therefore checks every supplied path before reporting the overall result.
Read the exit status
A command’s exit status is a number: zero conventionally signals success, while a nonzero value signals failure. Here, 0 means every argument passed the regular-file test, 1 means at least one did not, and 2 means no arguments were supplied. These meanings are defined by this script. Choose a different status scheme when the calling program needs to distinguish more failure cases. A tutorial for writing Bash scripts can use different nonzero codes to distinguish other failures.
Inspect the status immediately after a run, before another command overwrites $?:
./check-files.sh notes.txt
printf 'Exit status: %s\n' "$?"
This bash shell scripting tutorial also supports a common automation pattern: if ./check-files.sh notes.txt; then can select a success branch based on that status. Close the conditional with fi after adding its commands. Other programs can inspect the status without parsing the printed messages.
Diagnose a failed run
If the terminal reports Permission denied, add execute permission with chmod +x or invoke the file with bash. If it reports No such file or directory, check the script path and the shebang; Windows-style line endings can also prevent the interpreter from being found. If a filename with spaces produces separate error lines, quote it when passing it to the script. A successful check prints one OK line per file and returns status 0.
