Skip to content

Instantly share code, notes, and snippets.

@danydev
Last active June 2, 2026 11:23
Show Gist options
  • Select an option

  • Save danydev/bd09343bc1521ca2cac0a9f1ab611cc2 to your computer and use it in GitHub Desktop.

Select an option

Save danydev/bd09343bc1521ca2cac0a9f1ab611cc2 to your computer and use it in GitHub Desktop.
Update iTerm Badge with hostname of the server used to ssh
#!/bin/bash
# Script that updates the iTerm Badge with the hostname of the server that you are
# connecting to with ssh.
#
# Instructions:
# - Put this script in ~/bin/ssh (this will override the default ssh binary)
# - Run 'chmod +x ~/bin/ssh' to give execution permission to the script
# - Open iTerm\Preferences\Profiles, select your profile and put '\(user.current_ssh_host)' in the Badge text box
# - Enjoy!
#
# Troubleshoot issues:
# - If it's not working, make sure your shell is white-listed in the script (see $PARENT_COMMAND in the script)
#
# Credits: inspired by https://engineering.talis.com/articles/bash-osx-colored-ssh-terminal/
iterm2_set_user_var () {
PARENT_COMMAND=$(ps -o comm= $PPID)
# Avoid to do send the command when ssh is not run by the shell
if [ "$PARENT_COMMAND" = "bash" ] || [ "$PARENT_COMMAND" = "-zsh" ]; then
printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64 | tr -d '\n')
fi
}
on_exit () {
iterm2_set_user_var current_ssh_host ""
}
trap on_exit EXIT
HOSTNAME=`echo $@ | sed s/.*@//`
iterm2_set_user_var current_ssh_host "$HOSTNAME"
/usr/bin/ssh "$@"
@danydev

danydev commented Nov 16, 2019

Copy link
Copy Markdown
Author

Below an example of what you get with this script
Screenshot 2019-11-16 at 13 06 14

@aa53420

aa53420 commented Jun 2, 2026

Copy link
Copy Markdown

Great idea. This was exactly the feature I was looking for, and after trying it out, I'm very satisfied with it. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment