HOWTO · Bash

How to Remove Empty Line in Bash

This tutorial will discuss removing empty lines in Bash.

On this page

When a text file contains unwanted blank lines, you can filter them while displaying the file with sed, grep, or awk. Each command below writes the retained lines to standard output and leaves the source file unchanged. These are command-line utilities used from Bash or another POSIX-like shell, not Bash keywords or built-ins.

Choose sed for a stream-editing command, grep for a compact inverse match, or awk when the filter will become part of a larger record-processing script. To save the result, redirect the command’s output to a separate file and review it before replacing the source. The utilities are commonly available in Bash/Linux and other POSIX-like command-line environments.

Prepare the Bash Input File

For the command examples, assume 1_Test.txt contains four text lines with one blank line between the second and third lines. The complete script below creates that file at a temporary path, applies the filter, and prints the result. It removes its temporary files after the command finishes. The output block shows the four retained lines.

temp_dir="$(mktemp -d)"
sample_file="$temp_dir/1_Test.txt"
printf '%s\n' \
  'This is the first line.' \
  'This is the second line.' \
  '' \
  'This is the third line.' \
  'This is the fourth line.' > "$sample_file"
sed '/^[[:space:]]*$/d' "$sample_file"
rm -f "$sample_file"
rmdir "$temp_dir"
This is the first line.
This is the second line.
This is the third line.
This is the fourth line.

This removes blank lines from the displayed result while preserving the four nonblank lines.

The expression ^[[:space:]]*$ matches a line from its start to its end when it contains only whitespace. Therefore, the same commands handle both lines with no characters and lines containing spaces or tabs.

Filter Lines with the sed Utility

sed treats /^[[:space:]]*$/d as a deletion rule. When the pattern matches, d removes that input line from the stream sent to standard output. For the sample file, run sed '/^[[:space:]]*$/d' 1_Test.txt.

This command leaves the original file unchanged. If you need a new cleaned file, redirect the output to a separate path, for example sed '/^[[:space:]]*$/d' 1_Test.txt > cleaned.txt, and review it before replacing the source. Redirection is what saves the filtered stream; sed itself is still reading the original path.

Filter Lines with the grep Utility

grep -v reverses the match, so it prints lines that do not match ^[[:space:]]*$. For 1_Test.txt, run grep -v '^[[:space:]]*$' 1_Test.txt. The regular expression is the same as in the sed command, while -v tells grep to keep nonmatching lines.

Like the sed version, grep sends the retained lines to standard output and does not edit the input path. It is a convenient choice when the task is simply to exclude lines matching one pattern. Redirect its output to another file when you need a saved result.

This self-contained example creates the sample file before applying the grep filter, so it can be adapted without relying on an existing companion file:

temp_dir="$(mktemp -d)"
sample_file="$temp_dir/1_Test.txt"
printf '%s\n' \
  'This is the first line.' \
  'This is the second line.' \
  '' \
  'This is the third line.' \
  'This is the fourth line.' > "$sample_file"
grep -v '^[[:space:]]*$' "$sample_file"
rm -f "$sample_file"
rmdir "$temp_dir"

Filter Lines with the awk Utility

awk prints a record when its condition is true. The negated pattern ! /^[[:space:]]*$ is true for every line that contains more than whitespace, so awk '!/^[[:space:]]*$/' 1_Test.txt preserves the meaningful lines from 1_Test.txt.

This form is useful when the line filter will become part of a larger record-processing script. Like the other commands, it writes only the retained records to standard output and does not replace 1_Test.txt. Redirect the result or use a separate file-editing workflow when you need a saved copy.

The following standalone example uses the same input shape with awk and leaves the source path available for later commands:

temp_dir="$(mktemp -d)"
sample_file="$temp_dir/1_Test.txt"
printf '%s\n' \
  'This is the first line.' \
  'This is the second line.' \
  '' \
  'This is the third line.' \
  'This is the fourth line.' > "$sample_file"
awk '!/^[[:space:]]*$/' "$sample_file"
rm -f "$sample_file"
rmdir "$temp_dir"