You are not logged in.

#1 2023-11-10 07:06:42

mbw
Member
Registered: 2020-01-04
Posts: 38

[Solved] How can I avoid /usr/bin/sh truncating my bash_history?

I have the following entries in my .bashrc to try to ensure that I don't accidentally overwrite the history file by closing one of multiple long-lived shells:

```
shopt -s histappend
export PROMPT_COMMAND='history -n; history -a'  # This is supposed to allow interleaving histories, but I'm not sure if that is the case. Maybe replace -n with -r?
HISTSIZE=100000
HISTFILESIZE=100000
HISTCONTROL='ignoreboth:erasedups'
```

While this can improved on, at least it works reasonably well.

However, I noticed that /usr/bin/sh is a symlink to bash, and if I
```
$ sh
$ exit
$ cat ~/.bash_history | wc -l
```
, the history is truncated to 500 lines.

It appears that with sh, HISTSIZE=500, HISTFILE=$HOME/.bash_history.

I don't suppose I can just delete /usr/bin/sh? smile

Or rather, does bash invoked via the sh symlink respect some configuration file other than .bashrc?
Does it take cli arguments I should alias it with?

Last edited by mbw (2023-11-12 08:44:34)

Offline

#2 2023-11-10 08:48:38

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 76,064

Re: [Solved] How can I avoid /usr/bin/sh truncating my bash_history?

https://man.archlinux.org/man/core/bash … INVOCATION

If bash is invoked with the name sh

I don't suppose I can just delete /usr/bin/sh?

Oh, you /can/.
Do you /want/?
Probably not wink

This is all only relevant for interactive shells, though and /bin/sh is typically for scripts, nobody runs that as interactive shell b/c you don't get all your fancy interactive shell config.

Offline

#3 2023-11-10 10:48:57

frostschutz
Member
Registered: 2013-11-15
Posts: 1,647

Re: [Solved] How can I avoid /usr/bin/sh truncating my bash_history?

You could set an alias that starts sh without HISTFILE:

alias sh='HISTFILE= sh'

alternatively, in .bashrc you could set a nonstandard HISTFILE:

HISTFILE=~/.bash_history_custom

and sh would keep using .bash_history anyway since it doesn't load the bashrc at all

or you could try setting HISTSIZE globally in /etc/environment or something, not sure if that would work if it's just about truncate behavior. or just set them in bashrc with export ...

# without export:
$ HISTSIZE=10000
$ sh
sh-5.2$ echo $HISTSIZE
500

# with export:
$ export HISTSIZE=10000
$ sh
sh-5.2$ echo $HISTSIZE
10000

Last edited by frostschutz (2023-11-10 10:52:44)

Offline

#4 2023-11-12 08:44:02

mbw
Member
Registered: 2020-01-04
Posts: 38

Re: [Solved] How can I avoid /usr/bin/sh truncating my bash_history?

Thank you, I believe just exporting the history-related envvars should be enough.

While one would not normally use sh interactively, I accidentally truncated my history several times, without knowing how.
I am now pretty sure it was because of this, and I believe I started sh to try out some commands for writing a DOCKERFILE.

Offline

Board footer

Powered by FluxBB