diff --git a/bashrc-forward.sh b/bashrc-forward.sh index d9d995f..54f293c 100644 --- a/bashrc-forward.sh +++ b/bashrc-forward.sh @@ -17,11 +17,15 @@ bashrc_get () { echo "set +v" } +_strip_comments () { + grep -v '^[[:space:]]*#\+-#' +} + bashrc_load () { if [ "$_s" ]; then #-# Relay .bashrc if it has already been forwarded : "${_s#*[B]ASHRC_FORWARDED=1[[:space:]]}" - printf %s "${_%[[:space:]][s]et +v*}" + printf '%s\n%s\n%s\n' "BASHRC_FORWARDED=1" "${_%[[:space:]][s]et +v*}" "set +v" else #-# Warning: Your `.bashrc` MUST NOT print any error messages at all! #-# Additionally, be aware that every time you use bashrc_forward, @@ -41,9 +45,10 @@ bashrc_load () { fi | #-# Removing comments (if marked as such by using #-#) because final `.bashrc` must be #-# below 131k bytes because of resource limits (`MAX_ARG_STRLEN` on Linux) - grep -v -e '^[[:space:]]*#\+-#' -e ' # *bashrc_forward: .*\[ a-z]*$' | + _strip_comments | + grep -v -e ' # *bashrc_forward: .*\[ a-z]*$' | sed ' - s,/nix/store/[^/]\+,, + s,/[n]ix/store/[^/]\+,, s,'\'','\''\\'\'''\''\\'\''\\'\'''\'''\''\\'\'''\'',g ' } @@ -55,6 +60,8 @@ bashrc_load () { #-# *unless you relay the `.bashrc`, in that case the remote system needs sed, grep, cat and bash. bashrc_forward () { printf %s ' + #-# If bashrc was forwarded using environment variable, unset it, since we do not need it anymore and to keep the env clean. Relaying will set it anyways to something fresh. + unset FORWARDED_BASHRC _argv_wrap () { if >/dev/null 2>&1 command -v bash; then bash -c '\'' @@ -80,12 +87,6 @@ bashrc_forward () { #-# 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\"'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\''" - #-# 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'\''\'\'''\''\'\''\'\'''\'''\''\'\'''\'' @@ -155,8 +156,9 @@ bashrc_forward () { fi fi } + #-# Allow accepting shell arguments _argv_wrap \ - ' + ' | _strip_comments } #-# HARD TO FIX BUG: shopt -u expand_aliases must be run in ENV so bash-abbr works, but ENV is run before the shell switches to interactive @@ -179,3 +181,5 @@ bashrc_forward () { alias brc='*"$(bashrc_forward)"' alias bbrc='*sh -c "$(bashrc_forward)"' alias tbrc='*-t "$(bashrc_forward)"' + +#-# TODO consider bash --rcfile <() approach, maybe it works better than ENV approach. Would also solve the "HARD TO FIX BUG" from above.