Have the Konsole's Title show the text of the active Konsole Tab
For the title to reflect the text of the currently selected Konsole tab, Konsole must be configured with the "Show window title on the titlebar" option disabled (i.e., unchecked). If this is not done, the code in set-konsole-active-tab-text.sh, near the bottom of this answer, will only be able to change the Konsole tab text, but the Konsole title will not be affected and will retain its value when the code is executed.
In fact, if you see this behavior, the tab changing but the title staying fixed at its former value, it is a fairly good indication that the "Show window title on the titlebar" configuration option is set for the current Konsole instance.
To set this property, open the Konsole General Configuration using the the Settings/Configure Konsole menu item. Make sure that the top "General" item is selected on the left and under the General settings on the right and then either set or confirm that the "Show window title on the titlebar" configuration item is unchecked.
Set the Konsole Title to a Fixed String (independent of tabs)
On the other hand, if you desire to simply set the Konsole Title and keep it independent of the text in the currently selected tab, you can set/leave the "Show window title..." option checked. Then, you can use the following code snippet to set the Konsole Title to a fixed setting and keep it independent of any prior/subsequent changes to Konsole tab text settings for all current and newly created tabs in the window:
set-konsole-title.sh
#!/usr/bin/env bash
# Script: set-konsole-title.sh
# Author: Michael Goldshteyn
# Last modified: 2022-07-26
# Prerequisites
# Requires KDE Plasma 5 or higher
# The "Show window title on the titlebar" configuration option
# must be enabled (checked "on," in Konsole Config/General)
# Note: If no argument is passed to the script, the Konsole title
# will get set to an empty string.
konsole_title="$1:-"
# Use an ANSI escape code to change the Konsole Title
printf $'\033]2;%s\007' "$konsole_title"
Title/Tab Text association
I would like to reiterate that thef following method, assuming "Show window title on the titlebar" is unchecked as previously described, not only allows for programmatic tab text changes but also dynamically ties the title of the Konsole window to the text found in the currently active tab. This implies that the Konsole title will be dynamic, changing as you move between tabs, updated to reflect the text of the tab made active during tab activation and thereafter staying in sync with the text of this tab (as long as it stays active).
The following method provides the ability to set the tab text of the currently active tab programmatically, perhaps to a fixed string, and then have the Konsole title optionally reflect the text as displayed in the currently active tab.
Bonus Tip
The initial text for a newly opened tab can be specified in the Konsole's Tabs Configuration Section - under the Settings menu, select Configure Konsole as for the prior point and select the Tabs configuration section on the left-hand side of the Configure - Konsole dialog box that opens.
Cont...
If you notice that your Konsole title is (strangely) reverting to the display of the default tab text, it is most likely due to the following script not being run for a newly opened tab (to change its text to the text you prefer).
Programmatically Changing the Text of the Active Konsole Tab
Here is a script showing a way to accomplish this, beginning with KDE Plasma 5, as a fully commented stand-alone Bash shell script you can add to your collection of useful utility scripts:
set-konsole-active-tab-text.sh
#!/usr/bin/env bash
set -x
# Script: set-konsole-active-tab-text.sh
# Author: Michael Goldshteyn
# Last modified: 2022-07-26
# A Bash script that can be used to set the text of the currently
# active tab of a Konsole process, optionally updating the Konsole
# window's title in the window caption as a side-effect, and keeping
# the two in sync.
######################### Prerequisites #########################
# KDE Plasma 5 or higher
#
# Optional:
# The "Show window title on the titlebar" option must be disabled
# (i.e., unchecked) under the Konsole Configuration/General
# section, for the Konsole title text to stay synchronized with
# the text of the currenly active Konsole tab.
#
# Link to the Stack Exchange (Super User) answer with the latest
# version of this code: https://superuser.com/a/1733814/52267
#################################################################
set_konsole_tab_text()
{
local -i starting_pid
local -i pid_to_check
local -i ppid_to_check
local exe_filename_of_parent_proc
local -i tab_owner_konsole_pid
local dbus_service_name
local dbus_path
local -i role
local tab_text
# If no tab text is passed in, the tab text will be set to the
# empty string
tab_text="$1"
# The following code is necessary even if no tabs are visible,
# because Konsole suppresses the display of tabs when only
# a single tab is present. At least one tab is always open
# while a Konsole process is running. When more than one
# tab is open, the tabs will be shown and the text of all
# displayed tabs will become visible.
# Find the PID of the Konsole process that owns the currently
# selected tab, by walking up the process ancestor chain of
# the shell process running in the context of the current
# tab (i.e., whatever process is executing this code).
# Important Note: We may need to iterate up the process chain
# multiple times, if the following code is being executed
# within a subshell (e.g., if this code is placed into a
# shell script that gets executed within a separate (child)
# instance of Bash).
# Get the process id of the current (shell) process
starting_pid="$$"
pid_to_check="$starting_pid"
# Seek out our Konsole ancestor process
until [[ -n $tab_owner_konsole_pid ]]; do
# Get the PID of the parent of the current process in the chain
ppid_to_check="$(ps -p "$pid_to_check" -o ppid= | tr -d ' ')"
# Get the name of the associated executable (i.e., app filename)
exe_filename_of_parent_proc="$(ps -p "$ppid_to_check" -o comm=)"
# Is it an instance of the Konsole app?
if [[ "$exe_filename_of_parent_proc" == konsole ]]; then
# Eureka - we found it!
tab_owner_konsole_pid="$ppid_to_check"
else # No, keep searching up the process ancestor chain
pid_to_check="$ppid_to_check" # ..., from the parent process
fi
done
# Construct the name of the qdbus service using the above
dbus_service_name="org.kde.konsole-$tab_owner_konsole_pid"
# The $KONSOLE_DBUS_SESSION environment varable gets set
# automatically by Konsole (and gets exported to the child
# shell associated with the tab).
# This value should be used for the dbus_path argument to qdbus
dbus_path="$KONSOLE_DBUS_SESSION"
# Use this for the role arg of the setTitle() method, below
role=1
# Set the tab text using a dbus method call
qdbus "$dbus_service_name"\
"$dbus_path"\
setTitle "$role" "$tab_text"
}
# Set the text of the currently active Konsole tab to the text
# passed as an argument to this script
set_tab_text "$@"