Replace a Config Value
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
# app settingsport=8080debug=falselog_level=info# port=7070 is only used in testsdb_host=localhostDo this to app.conf, in this order:
- Print the file before the change.
- Change
portto9090anddebugtotruein the file itself, and keep the original asapp.conf.bak. - Add
timeout=30at the end, but only if there is notimeout=line yet. - Print the file after the change, then the old
portanddebuglines from the backup.
Expected output:
== before ==# app settingsport=8080debug=falselog_level=info# port=7070 is only used in testsdb_host=localhost== after ==# app settingsport=9090debug=truelog_level=info# port=7070 is only used in testsdb_host=localhosttimeout=30== the backup keeps the old values ==port=8080debug=falseHints
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.
| 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.
cat app.confprints the file before the change.- The
sed -i.bakcommand rewrites theportanddebuglines. The comment line# port=7070 is only used in testsis unchanged, because it does not start withport=. - The
grep -q ... || echo ... >> app.confline addstimeout=30at the end, because the file had notimeout=line. - The last
grep -E '^(port|debug)='reads the backup and shows the old values,8080andfalse.
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.bakRecapThe 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, andgrep -q '^key=' file || echo 'key=value' >> fileto 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-iwith 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 matchesport=8080andport = 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
findrunsedon 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 onesedcall. Each file gets its own.bak, and any file without aport=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.