Rasmus Villemoes 9ca5488046 profile/systemd-osc-context: do not use sed for escaping
Bash is perfectly capable of performing the simple substitutions needed for
escaping according to the OSC 3008 spec, so there is no need for the fork+exec
and other overhead of calling sed.

In fact, when writing a test for ensuring that this is a drop-in replacement, I
found out that the current sed method is flawed: If $PWD contains newline
characters, they are passed through unchanged, because sed obviously is
line-oriented.

I do not know which bash version started supporting
${foo//pattern/replacement}, but I ran the below on all Debian images from docker
hub going back to Debian 6 (EOL 2016), carrying BASH_VERSION =
4.1.5(1)-release, and they all succeeded. Since the logic otherwise relies on
PROMPT_COMMAND being an array variable, which happened in 5.1, this should be
all good.

=== test.sh ===
#!/bin/bash

echo "BASH_VERSION = $BASH_VERSION"
ret=0

using_sed() {
    echo "$1" | sed -e 's/\\/\\x5c/g' -e 's/;/\\x3b/g' -e 's/[[:cntrl:]]/⍰/g'
}

pure_bash() {
    local str="$1"
    str="${str//\\/\\x5c}"
    str="${str//;/\\x3b}"
    str="${str//[[:cntrl:]]/⍰}"
    printf "%s" "${str}"
}

do_test() {
    local r0="$1"
    local r1="$(using_sed "$r0")"
    local r2="$(pure_bash "$r0")"
    local s0="$(printf '%s' "$r0" | od -A x -t x1z -w40 | head -n1)"
    local s1="$(printf '%s' "$r1" | od -A x -t x1z -w40 | head -n1)"
    local s2="$(printf '%s' "$r2" | od -A x -t x1z -w40 | head -n1)"
    if [ "$r1" != "$r2" ] || [ "$s1" != "$s2" ] ; then
        echo "Input: $s0"
        echo "sed:   $s1"
        echo "bash:  $s2"
        ret=1
    fi
}

do_test ''
do_test "one ; semicolon"
do_test "two ; semicolons ;"
do_test 'bs \ sc ; bs \ sc ;'
do_test $'\t'
do_test $'sc ; cntrl \x01 \x02 \x03 \x1f \t bs \\ bs \\ sc ; '
do_test $'sc ; tab \x09 del \x7f  bs \\ ;'

# The last cases show that the existing function doesn't actually work in
# the case of $PWD containing a newline character, because sed is
# line-oriented, so a newline character will never be replaced.
if [ "$1" = "all" ] ; then
    do_test $'embedded \n newline'
    do_test $'ending in newline\n'
fi

exit "$ret"
=== test.sh ===
2026-07-21 11:06:26 +02:00
2026-07-20 12:40:02 +01:00
…
2026-07-08 08:23:04 +02:00
2026-04-18 14:24:47 +01:00
…
…
2026-06-17 09:59:34 +00:00
2025-06-05 14:39:20 +02:00
2026-03-06 08:55:55 +01:00
2025-10-07 13:00:12 +01:00
…
2026-07-20 15:03:37 +09:00

Systemd

System and Service Manager

OBS Packages Status
Semaphore CI 2.0 Build Status
Coverity Scan Status
OSS-Fuzz Status
CIFuzz
CII Best Practices
Fossies codespell report
Translation status
Coverage Status
Packaging status
OpenSSF Scorecard

Details

Most documentation is available on systemd's web site.

Assorted, older, general information about systemd can be found in the systemd Wiki.

Information about build requirements is provided in the README file.

Consult our NEWS file for information about what's new in the most recent systemd versions.

Please see the Code Map for information about this repository's layout and content.

Please see the Hacking guide for information on how to hack on systemd and test your modifications.

Please see our Contribution Guidelines for more information about filing GitHub Issues and posting GitHub Pull Requests.

When preparing patches for systemd, please follow our Coding Style Guidelines.

If you are looking for support, please contact our mailing list, join our IRC channel #systemd on libera.chat or Matrix channel

Stable branches with backported patches are available in the stable repo.

We have a security bug bounty program sponsored by the Sovereign Tech Fund hosted on YesWeHack

Repositories with distribution packages built from git main are available on OBS, and also repositories with packages built from the latest stable release

S
Description
systemd patched fork
Readme Cite this repository
409 MiB
Languages
C 88.5%
Shell 5.7%
Python 4.6%
Meson 1.1%