-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.bashrc
More file actions
92 lines (76 loc) · 3.28 KB
/
Copy path.bashrc
File metadata and controls
92 lines (76 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# .bashrc
log() {
local file="${BASH_SOURCE[1]:-${BASH_SOURCE[0]}}"
printf "[%s %s] [%s] %s\n" "$(date +"%F %T.%6N")" "$$" "${file##*/}" "$*" >>/tmp/shell.log
}
# https://shreevatsa.wordpress.com/2008/03/30/zshbash-startup-files-loading-order-bashrc-zshrc-etc/
#
# +------------------+-------------+-------------+--------+
# | | Interactive | Interactive | Script |
# | | login | non-login | |
# +------------------+-------------+-------------+--------+
# | /etc/profile | A | | |
# +------------------+-------------+-------------+--------+
# | /etc/bash.bashrc | | A | |
# +------------------+-------------+-------------+--------+
# | ~/.bashrc | | B | |
# +------------------+-------------+-------------+--------+
# | ~/.bash_profile | B1 | | |
# +------------------+-------------+-------------+--------+
# | ~/.bash_login | B2 | | |
# +------------------+-------------+-------------+--------+
# | ~/.profile | B3 | | |
# +------------------+-------------+-------------+--------+
# | BASH_ENV | | | A |
# +------------------+-------------+-------------+--------+
# | | | | |
# +------------------+-------------+-------------+--------+
# | logout only: | | | |
# +----------------- + ----------- + ----------- + ------ +
# | ~/.bash_logout | C | | |
# +------------------+-------------+-------------+--------+
if [[ "$BASH_VERSINFO" = "3" ]]; then
# 这里专门写给 Bash 3.2 用的覆盖逻辑
export PS1="\u@\h \w \$ "
fi
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# shellcheck source=.config/shell/env
. ~/.config/shell/env
# Quick access to configuration files
# shellcheck disable=SC2139
alias shrc="${EDITOR} ~/.bashrc"
log "sourcing ~/.config/shell/rc.d/[0-9][0-9]*.sh"
for rcfile in ~/.config/shell/rc.d/[0-9][0-9]*.sh; do
# shellcheck disable=SC2086
# shellcheck source=/dev/null
. $rcfile
done
log "sourced"
alias dotfiles='git --git-dir=$HOME/.dotfiles --work-tree=$HOME'
alias dotlocal='git --git-dir=$HOME/.dotlocal --work-tree=$HOME/.local'
# 场景:从 zsh 中执行了 bash 命令
if [[ -n "$HISTFILE" && "$HISTFILE" == *.zsh_history ]]; then
ZSH_HIST=$HISTFILE
BASH_HIST=~/.bash_history
# 1. 启动时:先加载 Bash 历史
export HISTFILE="$BASH_HIST"
[[ -f "$BASH_HIST" ]] && history -r
# 2. 清理 zsh history 格式,追加 Zsh history 到 bash history 内存(显式加载临时文件)
tmplog="/tmp/zsh_sess_$$.log"
sed 's/^: [0-9]*:[0-9]*;//' "$ZSH_HIST" | tail -n 500 >"$tmplog"
history -r "$tmplog"
rm -f "$tmplog"
# 3. 实时追加当前 session 新产生的 Bash 命令
shopt -s histappend
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
# 4. 退出时:清空内存,防止 Bash 把内存里加载的 Zsh 记录写进 HISTFILE
cleanup_before_exit() {
history -c
}
trap cleanup_before_exit EXIT
fi
# shellcheck source=/dev/null
[[ -f ~/.bashrc.local ]] && . ~/.bashrc.local