[PATCH 1/2] scripts/config: Use POSIX standard ERE (-E) in sed
From: Rong Zhang
Date: Sat Jun 06 2026 - 18:13:13 EST
The use of Extended Regular Expressions was removed by commit
83e8b90e1d2c ("scripts/config: use sed's POSIX interface").
Before that, the script used `-r' to enable ERE, which is indeed
non-portable. However, POSIX.1-2024 [1][2] has accepted `-E' as a
standard option to use ERE for matching, and major sed implementations
(GNU, FreeBSD, OpenBSD, NetBSD, macOS) have supported `-E' for over two
decades, so it makes no sense to use Basic Regular Expressions any more.
Switch to ERE to get rid of chained calls to sed. A rough benchmark with
~1000 editions showed a 40.0% speedup (8.78s => 5.27s, GNU sed). The
FreeBSD sed showed a similar speedup.
Link: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/sed.html [1]
Link: https://austingroupbugs.net/view.php?id=528 [2]
Signed-off-by: Rong Zhang <i@xxxxxxxx>
---
scripts/config | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)
diff --git a/scripts/config b/scripts/config
index ea475c07de28..1f290372a4ce 100755
--- a/scripts/config
+++ b/scripts/config
@@ -76,7 +76,7 @@ txt_append() {
# sed append cmd: 'a\' + newline + text + newline
cmd="$(printf "a\\%b$insert" "\n")"
- sed -e "/$anchor/$cmd" "$infile" >"$tmpfile"
+ sed -E -e "/$anchor/$cmd" "$infile" >"$tmpfile"
# replace original file with the edited one
mv "$tmpfile" "$infile"
}
@@ -87,7 +87,7 @@ txt_subst() {
local infile="$3"
local tmpfile="$infile.swp"
- sed -e "s$SED_DELIM$before$SED_DELIM$after$SED_DELIM" "$infile" >"$tmpfile"
+ sed -E -e "s$SED_DELIM$before$SED_DELIM$after$SED_DELIM" "$infile" >"$tmpfile"
# replace original file with the edited one
mv "$tmpfile" "$infile"
}
@@ -97,7 +97,7 @@ txt_delete() {
local infile="$2"
local tmpfile="$infile.swp"
- sed -e "/$text/d" "$infile" >"$tmpfile"
+ sed -E -e "/$text/d" "$infile" >"$tmpfile"
# replace original file with the edited one
mv "$tmpfile" "$infile"
}
@@ -105,14 +105,12 @@ txt_delete() {
set_var() {
local name=$1 new=$2 before=$3
- name_re="^($name=|# $name is not set)"
+ name_re="^($name=.*|# $name is not set)"
before_re="^($before=|# $before is not set)"
if test -n "$before" && grep -Eq "$before_re" "$FN"; then
- txt_append "^$before=" "$new" "$FN"
- txt_append "^# $before is not set" "$new" "$FN"
+ txt_append "$before_re" "$new" "$FN"
elif grep -Eq "$name_re" "$FN"; then
- txt_subst "^$name=.*" "$new" "$FN"
- txt_subst "^# $name is not set" "$new" "$FN"
+ txt_subst "$name_re" "$new" "$FN"
else
echo "$new" >>"$FN"
fi
@@ -121,8 +119,7 @@ set_var() {
undef_var() {
local name=$1
- txt_delete "^$name=" "$FN"
- txt_delete "^# $name is not set" "$FN"
+ txt_delete "^($name=|# $name is not set)" "$FN"
}
FN=.config
--
2.53.0