Shell Save command history in sh terminal

Hi All,
I am trying to save history in FreeBSD 14.2 sh terminal because it does not appear to be saving history across sessions. Below is the code and resulting .profile file
Code:
# Save sh terminal command history
echo 'HISTFILE=~/.sh_history' >> /home/michael/.profile
echo 'HISTSIZE=1000' >> /home/michael/.profile
echo 'trap "history -a" EXIT' >> /home/michael/.profile
echo 'history -r "$HISTFILE"' >> /home/michael/.profile
chown michael:michael /home/michael/.profile

Shell:
Code:
michael@cs:~ $ echo $SHELL
/bin/sh
.profile file:
Code:
#
# .profile - Bourne Shell startup script for login shells
#
# see also sh(1), environ(7).
#

# These are normally set through /etc/login.conf.  You may override them here
# if wanted.
# PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$HOME/bin; export PATH

# Setting TERM is normally done through /etc/ttys.  Do only override
# if you're sure that you'll never log in via telnet or xterm or a
# serial line.
# TERM=xterm;     export TERM

EDITOR=vi;       export EDITOR
PAGER=less;      export PAGER

# set ENV to a file invoked each time sh is started for interactive use.
ENV=$HOME/.shrc; export ENV

# Let sh(1) know it's at home, despite /home being a symlink.
if [ "$PWD" != "$HOME" ] && [ "$PWD" -ef "$HOME" ] ; then cd ; fi

# Query terminal size; useful for serial lines.
if [ -x /usr/bin/resizewin ] ; then /usr/bin/resizewin -z ; fi

# Display a random cookie on each login.
if [ -x /usr/bin/fortune ] ; then /usr/bin/fortune freebsd-tips ; fi
HISTFILE=~/.sh_history
HISTSIZE=1000
trap "history -a" EXIT
history -r "$HISTFILE"

However, still the terminal does not record the command history.

Thanks & Best Regards
AMJS
 
Some month go sh has been updated with a history function.For curiosity I have tried its function.
fc -l lists the last commands. history -a appends to the history which can also be monitored by checking the histfile. The default is ~/.sh_history.

The strange thing is that history works but triggers an error message. It is also not mentioned in sh().

EDIT: correction, it is not .sh:history but .sh_history
 
history -a appends to the history which can also be monitored by checking the histfile.
Ah, as SirDice mentioned, I missed the alias from the default ~/.shrc. Nevertheless, the OP's config is clearly wrong, as fc doesn't have appending/reading functionality similar to history -a|-r in bash(1). Furthermore, the whole history capability in sh(1) should work without any additional configuration ( HISTFILE and HISTSIZE defaults to ~/.sh_history and 100, respectively).
 
Hi All,
I am trying to save history in FreeBSD 14.2 sh terminal because it does not appear to be saving history across sessions. Below is the code and resulting .profile file

Code:
# Save sh terminal command history
echo 'HISTFILE=~/.sh_history' >> /home/michael/.profile
echo 'HISTSIZE=1000' >> /home/michael/.profile
echo 'trap "history -a" EXIT' >> /home/michael/.profile
echo 'history -r "$HISTFILE"' >> /home/michael/.profile

Try to put these commands in ~/.shrc instead of ~/.profile, I am not sure that ~/.profile is meant for that.
 
Looks like almost everything went wrong :)
  • The specified commands do nothing useful;
  • If they did something useful, they're still in the wrong place (credit to gotnull);
  • If the configuration itself were OK, ~/.sh_history is still a directory (credit to Andriy);
  • If it were a file, it's still owned by root (credit to covacat);
  • Finally, SIGHUP isn't catched by the shell itself, so in some cases the history won't be saved anyway (credit to chrbr).
Schroter, you can fix these issues as follows:
  • Delete/rename your ~/.sh_history directory;
  • Remove last 4 lines from your ~/.profile;
  • If you need more than 100 history items, add HISTSIZE=1000 to the ~/.shrc;
  • Try to close the shell gracefully (e. g. press Ctrl-D or type exit).
 
Should it really be a directory?
eseipi provided the answer. I was not closing the terminal the proper way. (e. g. press Ctrl-D or type exit). That is why it was not saving terminal commands with the default settings. The default settings work just fine. Thanks
 
Hi,

I have the same problem. When I create one SSH session from my Mac to my FreeBSD server as myuser, then use su to become root, and then type exit, the history is saved for the next session.

However, when I create two terminal sessions to my FreeBSD server, the history is not saved properly, even when I exit both sessions using exit.

This scenario works

FreeBSD 14.3

  1. Open a macOS shell and log in to the FreeBSD server via SSH as user myfreebsd
  2. Type su to log in as root
  3. Type hello10000
  4. Type exit
  5. Type exit again
  6. Log in again as root and check history
Result: history is saved properly and hello10000 is there.

This more common scenario, at least for me, does not save history

  1. Open the FIRST macOS shell and log in to the FreeBSD server via SSH as user myfreebsd
  2. Open the SECOND macOS shell and log in to the FreeBSD server via SSH as user myfreebsd
  3. Type su to log in as root in the FIRST macOS shell
  4. Type su to log in as root in the SECOND macOS shell
  5. Type hello20000 in the FIRST shell
  6. Type exit, and then exit again, in the FIRST shell
  7. Type exit, and then exit again, in the SECOND shell
  8. Log in again as root and check history
Result: hello20000 is not there.

Can't report it to Bugzilla

I was sad that such basic functionality does not work in FreeBSD, and then I found the already existing bug for it:

https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=283310

Then I was even more sad that the bug is already 2 years old and nobody has fixed it yet.

So, 5 days ago, I sent an email to bugmeister@FreeBSD.org to have an account created, exactly as Bugzilla says. I wanted to add comments to this bug and make the scenario more precise.

Unfortunately, it has been 5 days and I still have not received an account.

So now I am very sad.
 
Back
Top