mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 07:46:12 +03:00
bash: less intrusive automatic shell integration
We now use a temporary function (__ghostty_bash_startup) to perform the bash startup sequence. This gives us a local function scope in which to store some temporary values (like rcfile). This way, they won't leak into the sourced files' scopes. Also, use `~/` instead of `$HOME` for home directory paths as a simpler shorthand notation.
This commit is contained in:
@ -20,14 +20,13 @@
|
|||||||
if [[ "$-" != *i* ]] ; then builtin return; fi
|
if [[ "$-" != *i* ]] ; then builtin return; fi
|
||||||
if [ -z "$GHOSTTY_RESOURCES_DIR" ]; then builtin return; fi
|
if [ -z "$GHOSTTY_RESOURCES_DIR" ]; then builtin return; fi
|
||||||
|
|
||||||
# When automatic shell integration is active, we need to manually
|
# When automatic shell integration is active, we were started in POSIX
|
||||||
# load the normal bash startup files based on the injected state.
|
# mode and need to manually recreate the bash startup sequence.
|
||||||
if [ -n "$GHOSTTY_BASH_INJECT" ]; then
|
if [ -n "$GHOSTTY_BASH_INJECT" ]; then
|
||||||
builtin declare ghostty_bash_inject="$GHOSTTY_BASH_INJECT"
|
# Store a temporary copy of our startup flags and unset these global
|
||||||
builtin unset GHOSTTY_BASH_INJECT ENV
|
# environment variables so we can safely handle reentrancy.
|
||||||
|
builtin declare __ghostty_bash_flags="$GHOSTTY_BASH_INJECT"
|
||||||
# At this point, we're in POSIX mode and rely on the injected
|
builtin unset ENV GHOSTTY_BASH_INJECT
|
||||||
# flags to guide is through the rest of the startup sequence.
|
|
||||||
|
|
||||||
# Restore bash's default 'posix' behavior. Also reset 'inherit_errexit',
|
# Restore bash's default 'posix' behavior. Also reset 'inherit_errexit',
|
||||||
# which doesn't happen as part of the 'posix' reset.
|
# which doesn't happen as part of the 'posix' reset.
|
||||||
@ -40,35 +39,40 @@ if [ -n "$GHOSTTY_BASH_INJECT" ]; then
|
|||||||
builtin unset GHOSTTY_BASH_UNEXPORT_HISTFILE
|
builtin unset GHOSTTY_BASH_UNEXPORT_HISTFILE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Manually source the startup files, respecting the injected flags like
|
# Manually source the startup files. See INVOCATION in bash(1) and
|
||||||
# --norc and --noprofile that we parsed with the shell integration code.
|
# run_startup_files() in shell.c in the Bash source code.
|
||||||
#
|
function __ghostty_bash_startup() {
|
||||||
# See also: run_startup_files() in shell.c in the Bash source code
|
builtin local rcfile
|
||||||
if builtin shopt -q login_shell; then
|
|
||||||
if [[ $ghostty_bash_inject != *"--noprofile"* ]]; then
|
|
||||||
[ -r /etc/profile ] && builtin source "/etc/profile"
|
|
||||||
for rcfile in "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.profile"; do
|
|
||||||
[ -r "$rcfile" ] && { builtin source "$rcfile"; break; }
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if [[ $ghostty_bash_inject != *"--norc"* ]]; then
|
|
||||||
# The location of the system bashrc is determined at bash build
|
|
||||||
# time via -DSYS_BASHRC and can therefore vary across distros:
|
|
||||||
# Arch, Debian, Ubuntu use /etc/bash.bashrc
|
|
||||||
# Fedora uses /etc/bashrc sourced from ~/.bashrc instead of SYS_BASHRC
|
|
||||||
# Void Linux uses /etc/bash/bashrc
|
|
||||||
# Nixos uses /etc/bashrc
|
|
||||||
for rcfile in /etc/bash.bashrc /etc/bash/bashrc /etc/bashrc; do
|
|
||||||
[ -r "$rcfile" ] && { builtin source "$rcfile"; break; }
|
|
||||||
done
|
|
||||||
if [[ -z "$GHOSTTY_BASH_RCFILE" ]]; then GHOSTTY_BASH_RCFILE="$HOME/.bashrc"; fi
|
|
||||||
[ -r "$GHOSTTY_BASH_RCFILE" ] && builtin source "$GHOSTTY_BASH_RCFILE"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
if builtin shopt -q login_shell; then
|
||||||
|
if [[ $__ghostty_bash_flags != *"--noprofile"* ]]; then
|
||||||
|
[ -r /etc/profile ] && builtin source "/etc/profile"
|
||||||
|
for rcfile in ~/.bash_profile ~/.bash_login ~/.profile; do
|
||||||
|
[ -r "$rcfile" ] && { builtin source "$rcfile"; break; }
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [[ $__ghostty_bash_flags != *"--norc"* ]]; then
|
||||||
|
# The location of the system bashrc is determined at bash build
|
||||||
|
# time via -DSYS_BASHRC and can therefore vary across distros:
|
||||||
|
# Arch, Debian, Ubuntu use /etc/bash.bashrc
|
||||||
|
# Fedora uses /etc/bashrc sourced from ~/.bashrc instead of SYS_BASHRC
|
||||||
|
# Void Linux uses /etc/bash/bashrc
|
||||||
|
# Nixos uses /etc/bashrc
|
||||||
|
for rcfile in /etc/bash.bashrc /etc/bash/bashrc /etc/bashrc; do
|
||||||
|
[ -r "$rcfile" ] && { builtin source "$rcfile"; break; }
|
||||||
|
done
|
||||||
|
if [[ -z "$GHOSTTY_BASH_RCFILE" ]]; then GHOSTTY_BASH_RCFILE=~/.bashrc; fi
|
||||||
|
[ -r "$GHOSTTY_BASH_RCFILE" ] && builtin source "$GHOSTTY_BASH_RCFILE"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
__ghostty_bash_startup
|
||||||
|
|
||||||
|
builtin unset -f __ghostty_bash_startup
|
||||||
|
builtin unset -v __ghostty_bash_flags
|
||||||
builtin unset GHOSTTY_BASH_RCFILE
|
builtin unset GHOSTTY_BASH_RCFILE
|
||||||
builtin unset ghostty_bash_inject rcfile
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Sudo
|
# Sudo
|
||||||
|
Reference in New Issue
Block a user