Skip to main content
Bash and Linux

Replace a Config Value

easysed and config editing Must-do

Problem statement

Change values in a config file in place with sed -i, keep a backup copy, and add a missing setting when it does not exist yet. Editing a config from a script is part of most deploys, and doing it safely means you only touch the line you mean to, leave comments alone and can undo the change.

app.conf

Bash
# app settings
port=8080
debug=false
log_level=info
# port=7070 is only used in tests
db_host=localhost

Do this to app.conf, in this order:

  1. Print the file before the change.
  2. Change port to 9090 and debug to true in the file itself, and keep the original as app.conf.bak.
  3. Add timeout=30 at the end, but only if there is no timeout= line yet.
  4. Print the file after the change, then the old port and debug lines from the backup.

Expected output:

TEXT
== before ==
# app settings
port=8080
debug=false
log_level=info
# port=7070 is only used in tests
db_host=localhost
== after ==
# app settings
port=9090
debug=true
log_level=info
# port=7070 is only used in tests
db_host=localhost
timeout=30
== the backup keeps the old values ==
port=8080
debug=false

Hints

Hint 1: sed 's/old/new/' replaces text on every line it matches. Starting the pattern with ^ means "at the start of the line", so a comment line such as # port=7070 can never match ^port=.

Approach

Optimal: sed -i.bak and a grep check

Covers: sed -i.bak, the s/old/new/ command, the anchor ^, .* for "the rest of the line", several -e commands, grep -q, || and >>.

Match the whole setting, then write it again. s/^port=.*/port=9090/ finds a line that starts with port=, takes everything after it with .*, and replaces the whole line with port=9090. The ^ is the safety catch: only a line that begins with port= changes.

%%{init: {"flowchart": {"padding": 18, "nodeSpacing": 30, "rankSpacing": 40, "htmlLabels": true}, "themeVariables": {"fontSize": "18px"}}}%% flowchart TB F["app.conf"]:::gray --> B(["sed -i.bak copies it to app.conf.bak"]):::purple B --> E["edit app.conf line by line"]:::blue E --> M{{"line starts with port= ?"}}:::blue M --> Y["rewrite it as port=9090"]:::green M --> N["leave the line as it is"]:::gray classDef blue fill:#dbeafe,stroke:#2563eb,color:#1e3a8a,stroke-width:2px classDef yellow fill:#fef3c7,stroke:#d97706,color:#78350f,stroke-width:2px classDef green fill:#d1fae5,stroke:#059669,color:#064e3b,stroke-width:2px classDef red fill:#fee2e2,stroke:#dc2626,color:#7f1d1d,stroke-width:2px classDef purple fill:#ede9fe,stroke:#7c3aed,color:#4c1d95,stroke-width:2px classDef gray fill:#f3f4f6,stroke:#6b7280,color:#111827,stroke-width:2px linkStyle default stroke:#94a3b8,stroke-width:2px
Part Means
-i.bak edit the file itself, and first save the original as app.conf.bak
-e '...' one editing command. Use -e several times to run several commands in one pass
s/old/new/ substitute: replace what matches old with new
^port= a line that starts with port=. A comment like # port=7070 does not start with it
.* any characters up to the end of the line, so the old value is replaced whatever it was

Why a backup. sed -i rewrites the file and there is no undo. -i.bak is the cheap safety net: if the edit is wrong, copy app.conf.bak back. Always write the suffix right after -i with no space. That form works in both GNU and macOS sed.

Add a setting only when it is missing. sed only changes lines that exist. If the key is not in the file, the replacement silently does nothing. So the script checks first. grep -q '^timeout=' app.conf searches without printing, and its exit code is 0 when it finds the line. The || means "run the next command only if the previous one failed", so echo 'timeout=30' >> app.conf runs only when grep found nothing. >> appends to the end of the file and never overwrites it.

Walking through the code. The # Setup: lines only create the sample file, so skip past them.

  1. cat app.conf prints the file before the change.
  2. The sed -i.bak command rewrites the port and debug lines. The comment line # port=7070 is only used in tests is unchanged, because it does not start with port=.
  3. The grep -q ... || echo ... >> app.conf line adds timeout=30 at the end, because the file had no timeout= line.
  4. The last grep -E '^(port|debug)=' reads the backup and shows the old values, 8080 and false.

Edge cases. If the key appears twice, both lines are changed. If the file has port = 8080 with spaces around the equals sign, ^port= does not match and nothing changes, so look at the real format before you write the pattern. Running the script a second time is safe: the values are rewritten to the same text and timeout=30 is not added twice.

# Setup: create the sample config in a fresh temporary folder
cd "$(mktemp -d)"
cat > app.conf << 'DATA'
# app settings
port=8080
debug=false
log_level=info
# port=7070 is only used in tests
db_host=localhost
DATA

echo "== before =="
cat app.conf

# Change two values in place, and keep the original as app.conf.bak
sed -i.bak -e 's/^port=.*/port=9090/' -e 's/^debug=.*/debug=true/' app.conf

# Add timeout=30 only if the key is missing
grep -q '^timeout=' app.conf || echo 'timeout=30' >> app.conf

echo "== after =="
cat app.conf

echo "== the backup keeps the old values =="
grep -E '^(port|debug)=' app.conf.bak
RecapThe whole problem in a few lines, for the night before
  • Spot it: "change a setting in a config file from a script"
  • Idea: sed -i.bak 's/^key=.*/key=value/' file, and grep -q '^key=' file || echo 'key=value' >> file to add a missing key
  • Cost: one pass over the file, O(n) time, constant memory
  • Trap: no ^ anchor so a comment is changed too, and -i with no backup (or the macOS suffix rule)

Interview follow-ups

  • The file has spaces around the equals sign, like port = 8080. How do you match both styles?

    Allow spaces with the class [[:space:]]*, which means "zero or more space or tab characters": sed -i.bak 's/^port[[:space:]]*=.*/port = 9090/' app.conf. It matches port=8080 and port = 8080, and the replacement writes one fixed style, so the file ends up consistent. Decide on a single output style and keep to it. If the file also has indented lines, add [[:space:]]* right after the ^.

  • Apply the same change to every .conf file under a folder.

    Let find run sed on the files it finds: find /etc/myapp -name '*.conf' -exec sed -i.bak 's/^port=.*/port=9090/' {} +. The {} stands for the file names, and + passes many files to one sed call. Each file gets its own .bak, and any file without a port= line is simply left as it was. Test it first with a copy of the folder, or without -i, so you see what would change before it does.

Frequently asked questions

On macOS, -i always expects a suffix argument, so sed -i 's/a/b/' file treats the pattern as the suffix and fails. GNU sed on Linux makes the suffix optional. The form sed -i.bak '...' file, with the suffix attached to -i, works on both. If you do not want a backup on macOS, write sed -i '' '...' file, but that form breaks on Linux, so scripts that must run on both should use -i.bak and delete the backup afterwards.

A slash ends the pattern, so s/^path=.*/path=/srv/new/ is a syntax error. Choose another delimiter: the character right after s is the delimiter, so s|^path=.*|path=/srv/new| works. Any character that is not in your text is fine, and | or # are the usual picks. The same goes for & in the new text, which means "the whole match" and needs a backslash in front of it if you want a real ampersand.

Single quotes stop the shell from replacing $VAR, so the variable is not seen. Use double quotes around the whole sed command: sed -i "s/^port=.*/port=$NEW_PORT/" app.conf. Check the value first, because a value with a slash or an ampersand will break the command, as in the question above. For values you do not control, a safer way is to write the line with printf into a new file and move it into place.