I want to have the benefits of bash history command lookup, but working on multiple hosts I ssh to. However, I don't have a home directory on most of the remotes due to security policy, so there is no ~/.bash_history there. export HISTFILE=/tmp/blah does not work for current session. Even if I had a file there, it should be sync'ed across multiple remotes through some local file.

A solution should probably look like this:

Through .ssh/config or alias or somehow, execute something around every ssh command, e.g.

scp .remote-history user@remote:/tmp/history
ssh user@remote
scp user@remote:/tmp/history .remote-history.add
cat .remote-history.add >> .remote-history

unless there is an existing tool that does something similar.


This script (remote_run) should do what you want. it could be made much prettier, but...


Run it like:

remote_run remote_hostname

When you exit, it will overwrite (so don't have two concurrent remote_runs to th same host or the last one win for whose history is saved) your local file ~/.bash_history-remote_hostname

# version 0.0.1

read -d '' RUN << EOF
export HISTTIMEFORMAT='%F %T - '
bash -i;
awk '{print \"HIST \"\$0}' \$HISTFILE;
ssh $1 "cat > $LOCALHISTFILE" < ~/.bash_history-$1
ssh -t $1 "$RUN" | tee /tmp/$USER-remote-history
grep '^HIST ' /tmp/$USER-remote-history | sed -e 's/^HIST //' -e 's/^M//' > ~/.bash_history-$1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.