Skip to content

Instantly share code, notes, and snippets.

@tomwassenberg
Last active December 27, 2022 20:01
Show Gist options
  • Select an option

  • Save tomwassenberg/361a5429ab56634885a90d524cd27519 to your computer and use it in GitHub Desktop.

Select an option

Save tomwassenberg/361a5429ab56634885a90d524cd27519 to your computer and use it in GitHub Desktop.
Shell snippet to share the output of your terminal with another user on the system.
#!/usr/bin/env -S bash -i
# Unofficial Bash strict mode
set \
-o errexit \
-o errtrace \
-o noglob \
-o nounset \
-o pipefail
IFS=$'\n\t'
shopt -s inherit_errexit
set_and_check_target_tty() {
# shellcheck disable=2016
read -rp 'Target TTY (target user can check with `tty`): ' TARGET_TTY
[[ -c ${TARGET_TTY} ]] ||
{
printf '%s\n' "${TARGET_TTY} is not a suitable TTY."
exit 1
}
}
set_and_check_target_tty
declare -a RC_COMMANDS
RC_COMMANDS+=(
"PS1='(${SHLVL}) ${PS1}';"
"exec &> >(sudo tee '${TARGET_TTY}');"
"printf '\n'"
)
exec bash --rcfile <(printf %s "${RC_COMMANDS[@]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment