The iTerm2 page on badges mentions that session.name can be changed with escape codes, but describes the method somewhat cryptically:
session.name
The current session's name. Defaults to the profile name. May be changed with the escape sequence OSC 1 ; name ST, or by editing the session title in View>Edit Current Session.
If you look further down the page at code describing how to change badges, we see a sort of Rosetta Store:
The badge itself may be set with the following escape sequence:
OSC 1337 ; SetBadgeFormat=Base-64 encoded badge format ST
Here's an example that works in bash:
# Set badge to show the current session name and git branch, if any is set. printf "\e]1337;SetBadgeFormat=%s\a" \ $(echo -n "\(session.name) \(user.gitBranch)" | base64)
We can infer from this that "OSC" is meant to stand in for "\e]" and "ST" for "\a". But even with that information, nothing works:
printf "\e]1;%s\a" "New Title"
echo -en "\e]1;New Title\a"
(If you know how to set the tab title, that's also of interest to me! But I don't see any indication that it's possible to do through escape codes.)