Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search Gists
Sign in Sign up

Instantly share code, notes, and snippets.

    • Star(1)You must be signed in to star a gist
    • Fork(0)You must be signed in to fork a gist
    Save BlueDrink9/0feef52ab21ff82cf1302c33e5a7c06d to your computer and use it in GitHub Desktop.
    Pass environment variables through to ssh/tmux each time you log in
    This is a working solution I have found to the problem of updating values like whether you should use a powerline/nerd patched font,
    truecolors or what escape codes to use when sshed into a server that knows no specifics about your current terminal.
    SSH by default doesn't pass many variables through, with some caveats.
    Tmux, also, will not refresh any variables from the underlying shell.
    The way it works initally is by aliasing ssh to a function that first runs a command to export the specified options to the ssh shell,
    then runs the user's default shell.
    Tmux adds an extra layer to this. By default, it will integrate the shell variables on the **first run only**.
    Once it's running, and you create new sessions or reattach from a different terminal, nothing changes.
    To work around this, tmux is aliased to a function that first sets the running tmux's global env variables to the ones from
    the current shell, THEN attaches/runs. Then somewhere in .bashrc, a function is run that takes these tmux env variables and
    sets the shell variables from them. (These two functions are the same in the script, but branch depending on $TMUX.)
    Requires bash version >= 4 as shell, with this script sourced somewhere in the host `.bashrc`.
    ```sh
    # Any variable in this bash list will be exported to ssh and to tmux.
    export TERMOPTIONS=(USENF USEPF COLORTERM TERM_PROGRAM)
    # Set defaults here for various terms
    if substrInStr "Apple" "$TERM_PROGRAM"; then
    export COLORTERM=16
    fi
    if substrInStr "kitty" "$TERM"; then
    COLORTERM="truecolor"
    fi
    # Set a few defaults if I haven't overridden them in local .bashrc
    USENF=${USENF-0}
    USEPF=${USEPF-0}
    COLORTERM=${COLORTERM-16}
    TERM_PROGRAM=${TERM_PROGRAM-}
    generate_export_termoptions_cmd(){
    out=""
    for option in ${TERMOPTIONS[*]}; do
    out="${out} export ${option}=${!option}; "
    done
    echo "${out}"
    }
    ssh_with_options(){
    host=$1
    local EXPORT_TERMOPTIONS_CMD=`generate_export_termoptions_cmd`
    # Calls default shell, stripping leading '-'
    \ssh -t "$host" "${EXPORT_TERMOPTIONS_CMD} " '${0#-} -l -s'
    }
    alias ssh="ssh_with_options"
    set_tmux_termoptions(){
    for option in ${TERMOPTIONS[*]}; do
    # If attaching to a running tmux
    # session, we set a variable in tmux's global environment from
    # the containing shell. (This must be done before attaching to work!)
    # We then attach, and bash runs the refresh function.
    ## Set tmux environment
    if is_tmux_running; then
    # Check that we're in a session
    if [ ! -z "$TMUX" ]; then
    for option in ${TERMOPTIONS[*]}; do
    # Refresh termoption shell variables by parsing tmux's global environment
    local optval="$(\tmux show-environment -g ${option} 2>/dev/null)"
    export "${option}"="${optval##*=}"
    unset optval
    done
    else
    # Should this go after attaching tmux or before???
    tmux setenv -g "${option}" "${!option}"
    fi
    fi
    done
    }
    is_tmux_running(){
    # Check tmux is installed
    if command -v tmux>/dev/null; then
    # Check tmux has a session running
    if ! tmux ls 2>&1 | grep -q "no server running"; then
    return 0 # true
    fi
    fi
    return 1 # false
    }
    tmux_with_options(){
    set_tmux_termoptions
    \tmux "$@"
    }
    alias tmux="tmux_with_options"
    if [ ! -z "$TMUX" ]; then
    # In a tmux session
    set_tmux_termoptions
    fi
    ```
    Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

    [8]ページ先頭

    ©2009-2025 Movatter.jp