Skip to content

Commit 393952b

Browse files
committed
Add persistent bash history with multi-shell sync
1 parent 999e5e7 commit 393952b

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

scripts/bash/install.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env bash
2+
# Bash history installer. Runs as root.
3+
# - Drops history configuration into /etc/profile.d/ so every interactive shell
4+
# (login and non-login) picks it up.
5+
# - Strips the skeleton ~/.bashrc history block so it cannot override system values.
6+
# - Registers .bash_history with home-persist so it survives workspace rebuilds.
7+
set -eo pipefail
8+
9+
cat > /etc/profile.d/bash-history.sh <<'PROF'
10+
if [ -z "${__BASH_HIST_CONFIGURED:-}" ]; then
11+
__BASH_HIST_CONFIGURED=1
12+
HISTCONTROL=ignoreboth
13+
HISTSIZE=10000
14+
HISTFILESIZE=20000
15+
shopt -s histappend
16+
PROMPT_COMMAND="${PROMPT_COMMAND:+${PROMPT_COMMAND};}history -a; history -n"
17+
fi
18+
PROF
19+
20+
patch_bashrc() {
21+
local file="$1"
22+
[ -f "$file" ] || return 0
23+
24+
sed -i '/^# .*history/d' "$file"
25+
26+
if grep -q '^HISTCONTROL=' "$file"; then
27+
sed -i 's/^HISTCONTROL=.*/HISTCONTROL=ignoreboth/' "$file"
28+
else
29+
echo 'HISTCONTROL=ignoreboth' >> "$file"
30+
fi
31+
32+
if grep -q '^HISTSIZE=' "$file"; then
33+
sed -i 's/^HISTSIZE=.*/HISTSIZE=10000/' "$file"
34+
else
35+
echo 'HISTSIZE=10000' >> "$file"
36+
fi
37+
38+
if grep -q '^HISTFILESIZE=' "$file"; then
39+
sed -i 's/^HISTFILESIZE=.*/HISTFILESIZE=20000/' "$file"
40+
else
41+
echo 'HISTFILESIZE=20000' >> "$file"
42+
fi
43+
44+
if grep -q '^shopt -s histappend' "$file"; then
45+
sed -i '/^shopt -s histappend/d' "$file"
46+
fi
47+
}
48+
49+
patch_bashrc /etc/skel/.bashrc
50+
51+
home="${_REMOTE_USER:+/home/${_REMOTE_USER}}"
52+
[ -n "$home" ] && patch_bashrc "$home/.bashrc"
53+
54+
mkdir -p /etc/home-persist.d
55+
cat > /etc/home-persist.d/bash.json <<'EOF'
56+
{
57+
"source": "bash",
58+
"paths": [".bash_history"]
59+
}
60+
EOF

src/base/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ ENV _REMOTE_USER=${USERNAME}
8585
# would rebuild every tool.
8686
RUN --mount=type=bind,source=scripts/home-persist,target=/scripts/home-persist \
8787
bash /scripts/home-persist/install.sh
88+
RUN --mount=type=bind,source=scripts/bash,target=/scripts/bash \
89+
bash /scripts/bash/install.sh
8890
RUN --mount=type=bind,source=scripts/icu,target=/scripts/icu \
8991
bash /scripts/icu/install.sh
9092
RUN --mount=type=bind,source=scripts/gh,target=/scripts/gh \

0 commit comments

Comments
 (0)