From 57d1a9f0e21d1e9c9ac558e4e3040b678e6d382f Mon Sep 17 00:00:00 2001 From: cubernetes Date: Sat, 17 Oct 2026 01:22:07 +0200 Subject: [PATCH] Improve bash/tmuxb aliases --- bashrc-forward.sh | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/bashrc-forward.sh b/bashrc-forward.sh index 3e2161d..d9d995f 100644 --- a/bashrc-forward.sh +++ b/bashrc-forward.sh @@ -72,25 +72,20 @@ bashrc_forward () { set +o posix #-# In case `$0` is checked by other scripts BASH_ARGV0=bash + #-# The following host-side command substitution MUST be single line if this file where the bashrc_forward function is defined #-# is part of the host-side .bashrc. Otherwise the self-inclusion trick with the `set -v; cat ~/.bashrc; set +v` WILL NOT work, #-# either because of a bug in bash or because of a quirk I do not understand. For some reason, if it is multiline, only the line #-# where the command substition starts is printed. If there is a trailing backslash, then also the second line is printed. But in #-# no circumstance will the last line be printed, no matter what amount of trailing backslashes you add. '"$(bashrc_load)"' - #-# Not universal, but good enough to bash is preferrably invoked as sh so our constructed ENV is loaded. - alias bashb="bash -c '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''exec -a sh bash \"\$@\"'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' _" - #-# instead of the sudobash alias, you can also just do `sudo bbrc`. Keeping the `sudobash` alias anyway as an alternative. - #-# Warning: Be careful! sudobash will run YOUR .bashrc with ROOT on the remote system! You may or may not want this! - alias sudobash="ENV_RAW=\$(<\"\$ENV\") sudo --preserve-env=ENV_RAW bash --norc --noprofile -ic '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' - . <(printf %s \"\$ENV_RAW\") - unset -v ENV_RAW - builtin eval \"\$PROMPT_COMMAND\" - builtin exec -a sh bash \"\$@\" - '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' _" - #-# For tmux, it is more complicated. Here, the `ENV_RAW` workaround is actually required, because TMUX restricts the total argument lenght to around 16384 characters, often to little for a complex `.bashrc` - #-# If you need a login shell, add a `-l` to the last `bash` invocation - alias tmuxb="ENV_RAW=\$(<\"\$ENV\") tmux -2 new-session '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''bash --norc --noprofile -ic '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''\'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'''\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''. <(printf %s \"\$ENV_RAW\"); unset -v ENV_RAW; builtin eval \"\$PROMPT_COMMAND\"; builtin exec -a sh bash'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''\'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'''\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''" + + alias bash="-(exec -a sh bash {CURSOR})" + #-# tmux has a tight (16384) argument length limit. To bypass that limit, we pass the serialized `.bashrc` + + #-# via an environment variable and deserialize it using eval. Unfortunately, it does not work perfectly. + alias tmuxb="FORWARDED_BASHRC=\$(bashrc_forward) tmux -2 new-session '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''eval \"\$FORWARDED_BASHRC\"'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''" + #-# Using PROMPT_COMMAND instead of DEBUG trap because of speed tradeoffs (DEBUG trap runs way too often) #-# Also explicitly removing the export flag as to not append needlessly, in case the remote system exported it accidentally. declare +x PROMPT_COMMAND=$PROMPT_COMMAND'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''