diff --git a/bashrc-forward.sh b/bashrc-forward.sh index d9d995f..9978125 100644 --- a/bashrc-forward.sh +++ b/bashrc-forward.sh @@ -8,8 +8,9 @@ bashrc_get () { #-# Has some bugs, which is why we have to introduce this bashrc_get and that bashrc_load function, instead #-# of just embedding it inside the command substitution. echo "set -v" - #-# Marker and variable so we can slice out the original .bashrc for relaying and have custom login only for forwarded .bashrc - echo "BASHRC_FORWARDED=1" + #-# Setting BASH_COMPLETION_VERSINFO because nixos likes to hijack your .bashrc to run bash-completion.sh, which + #-# of course fails on remote systems, so we need a way to stop that from happening. Not perfect, but I'll accept the tradeoff. + echo "[ \"\${BASH_COMPLETION_VERSINFO-}\" ] || BASH_COMPLETION_VERSINFO=" #-# There's still a bug in bash at the moment regarding verbose mode not being turned on again after ${ :;} #-# https://www.mail-archive.com/bug-bash@gnu.org/msg35966.html #-# Therefore, be careful to use this new command substition syntax until it is fixed upstream! @@ -20,7 +21,7 @@ bashrc_get () { bashrc_load () { if [ "$_s" ]; then #-# Relay .bashrc if it has already been forwarded - : "${_s#*[B]ASHRC_FORWARDED=1[[:space:]]}" + : "${_s#*[B]ASH_COMPLETION_VERSINFO=[[:space:]]}" printf %s "${_%[[:space:]][s]et +v*}" else #-# Warning: Your `.bashrc` MUST NOT print any error messages at all! @@ -72,20 +73,25 @@ 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)"' - - 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\"'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''" - + #-# 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'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''\'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'''\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' '\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''" #-# 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'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''