Improve bash/tmuxb aliases

This commit is contained in:
cubernetes 2026-10-17 01:22:07 +02:00
parent e99f461ce3
commit 57d1a9f0e2

View file

@ -72,25 +72,20 @@ bashrc_forward () {
set +o posix set +o posix
#-# In case `$0` is checked by other scripts #-# In case `$0` is checked by other scripts
BASH_ARGV0=bash BASH_ARGV0=bash
#-# The following host-side command substitution MUST be single line if this file where the bashrc_forward function is defined #-# 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, #-# 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 #-# 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 #-# 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. #-# no circumstance will the last line be printed, no matter what amount of trailing backslashes you add.
'"$(bashrc_load)"' '"$(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 \"\$@\"'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' _" alias bash="-(exec -a sh bash {CURSOR})"
#-# instead of the sudobash alias, you can also just do `sudo bbrc`. Keeping the `sudobash` alias anyway as an alternative. #-# tmux has a tight (16384) argument length limit. To bypass that limit, we pass the serialized `.bashrc`
#-# 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 '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' #-# via an environment variable and deserialize it using eval. Unfortunately, it does not work perfectly.
. <(printf %s \"\$ENV_RAW\") alias tmuxb="FORWARDED_BASHRC=\$(bashrc_forward) tmux -2 new-session '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''eval \"\$FORWARDED_BASHRC\"'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''"
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'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''\'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'''\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''"
#-# Using PROMPT_COMMAND instead of DEBUG trap because of speed tradeoffs (DEBUG trap runs way too often) #-# 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. #-# 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'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' declare +x PROMPT_COMMAND=$PROMPT_COMMAND'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''