HOWTO · Bash

How to Replace Strings in Bash

This tutorial demonstrates how to replace one substring with another string in Bash. Learn effective methods using `sed`, `awk`, and Bash parameter expansion to enhance your string manipulation skills. Whether you're editing files or writing scripts, mastering these techniques will save you time and improve efficiency. Dive into practical examples and become proficient in Bash string replacements today!

On this page

Choose a replacement method based on where the text lives: Bash parameter expansion computes a replacement value from a variable in the current shell without modifying that variable, sed transforms lines in a stream, and awk is useful when replacement belongs in a record-processing step. Each example is self-contained, and file-processing examples create their own temporary input.

Choosing a replacement method

For a value already held in a Bash variable, parameter expansion avoids starting another process. Use sed for straightforward substitutions across input lines, especially when the input comes from a pipeline. Use awk when the same pass also needs field, record, or conditional logic. A command that reads a file and redirects its result to another file preserves the source; an in-place option changes the source and deserves extra care.

The examples use the literal text old as the search pattern and new as its replacement. These forms do not automatically treat the search text as a regular expression in Bash parameter expansion, but sed and awk have their own pattern syntax. If the input or replacement contains a delimiter used by a tool, choose and escape a suitable delimiter for that tool.

Replace the first or every match with Bash parameter expansion

Bash parameter expansion replaces the first matching pattern with ${value/pattern/replacement}. Add a second slash to replace every match: ${value//pattern/replacement}. In these examples, value is assigned locally, so the original variable remains unchanged and the result is stored in updated.

Run this example:

value='red old blue old'
updated="${value/old/new}"
printf '%s\n' "$updated"
red new blue old

To replace all matches in the same value:

value='red old blue old'
updated="${value//old/new}"
printf '%s\n' "$updated"
red new blue new

The pattern is a Bash pattern, so characters such as * have pattern meaning. Quote or otherwise handle patterns carefully when they come from variables or user input. Parameter expansion acts on one shell value; it does not edit a file by itself.

Match a pattern at the end of a string

The percent form ${value%pattern} removes the shortest suffix matching pattern; ${value%%pattern} removes the longest matching suffix. It does not generally mean “find the last occurrence” inside a string. To replace a known suffix, test or transform that ending explicitly. For example, replacing a .txt suffix can be expressed with a pattern that matches that ending:

filename='report.txt'
updated="${filename%.txt}.md"
printf '%s\n' "$updated"
report.md

This operation only concerns a matching suffix. A .txt sequence earlier in the name is not selected by this suffix removal pattern. If names may have other endings, check the expected suffix before applying the transformation so that a nonmatching name is handled deliberately.

Replace text in streams and files with sed

The sed substitution form s/pattern/replacement/ changes the first match on each input line. Add g after the final delimiter to replace every match on each line. This standalone command reads a file and writes the transformed text to standard output:

input=$(mktemp)
trap 'rm -f "$input"' EXIT
printf '%s\n' 'old road, old bridge' > "$input"
sed 's/old/new/g' "$input"
new road, new bridge

To save the result without changing the source, redirect output to a different file, such as output.txt. Avoid using the same path for input and redirected output: the shell opens the destination before sed reads, which can truncate the source. The -i option edits a file in place, and its backup syntax varies across sed implementations. First inspect the command on a copy or use a separate output file; make a backup when an in-place change is necessary.

In a sed expression, / is the usual delimiter. If a search or replacement includes it, select another delimiter such as | and escape delimiter characters that still occur in the expression. sed patterns are regular expressions, so literal punctuation may also need escaping.

Use awk for record-oriented replacements

awk can replace occurrences while processing each input record. gsub changes all matches in the selected string and returns the number of substitutions; $0 denotes the current record. This command prints transformed records, while leaving the input file alone:

input=$(mktemp)
trap 'rm -f "$input"' EXIT
printf '%s\n' 'old road, old bridge' > "$input"
awk '{ gsub(/old/, "new", $0); print }' "$input"
new road, new bridge

Redirect the output to a new path when you want a transformed file, and check that the destination is distinct from the input. awk regular-expression and replacement-string rules apply, so characters with special meaning need handling when patterns are not fixed literals. Choose awk when replacement is part of record-oriented processing; for a simple stream substitution, sed may be easier to read.

Common pitfalls and practical guidance

For multiple replacements in a variable, chain parameter expansions by assigning each result to the next value, or use one sed or awk pass with multiple substitutions. Keep each transformation explicit so the order is clear: an earlier replacement can create text that a later pattern then matches.

Use parameter expansion when the text is already in a Bash variable and the patterns are simple. It avoids an external command, but no general performance claim should replace measuring the workload that matters. sed is a direct fit for line-oriented stream substitutions. awk is useful when the replacement is combined with fields, conditions, or other record processing. These tools have different pattern and replacement rules, so do not assume that a pattern written for one can be copied unchanged into another.

Before changing a file, preserve the original by writing to a separate destination or making a backup. Confirm the input and output paths, review representative input, and only then use an in-place edit if that is required. This is especially important for broad patterns, where a global substitution can affect every matching occurrence on every line.

Run the relevant example with a copy of your data and inspect its result before applying it to important files. The article supplies no measured comparison between these methods, so choose by the input shape, transformation needs, and file-safety requirements rather than an assumed speed ranking.