Enable bash history

I am using LuCI openwrt-19.07 on my Archer C7 and I see that there's no bash history functionality. Is there a way to enable it and possibly keep the .bash_history file in my mounted external storage?

Do you really use bash? LuCI is a web frontend.

The standard is

# echo $SHELL
/bin/ash

Almquist shell

From Wikipedia, the free encyclopedia

Almquist shell (also known as A Shell , ash and sh ) is a lightweight Unix shell originally written by Kenneth Almquist in the late 1980s. Initially a clone of the System V.4 variant of the Bourne shell, it replaced the original Bourne shell in the BSD versions of Unix released in the early 1990s.

and

Embedded Linux

Ash (mainly the dash fork) is also fairly popular in embedded Linux systems. dash version 0.3.8-5 was incorporated into BusyBox, the catch-all executable often employed in this area, and is used in distributions like DSLinux, Alpine Linux, Tiny Core Linux and Linux-based router firmware such as OpenWrt, Tomato and DD-WRT.

4 Likes

Ok yes is there any solution for ash?

https://openwrt.org/packages/pkgdata/bash

1 Like

ash is provided by busybox, which provides all basic shell & Linux default app features in OpenWrt.

You can enable the history feature in busybox and recompile a personal version of it for yourself, so that you can have ash that remembers history. (I do that for my own build.)

1 Like

Doesn't that wear out the flash fast? On my laptop .bash_history is around 20k, which means each time it's updated a new block of flash has to be erased, I think?

I only store it on ramdisk, so I lose the history if the router reboots.

But if it is stored to flash, you could also config it to save only when the ash shell exits, not after each command.

https://github.com/openwrt/openwrt/blob/0e8ddc953f844861b60debec4d7c65f9b4de7434/package/utils/busybox/config/libbb/Config.in#L180

config BUSYBOX_CONFIG_FEATURE_EDITING_SAVEHISTORY
	bool "History saving"
	default BUSYBOX_DEFAULT_FEATURE_EDITING_SAVEHISTORY
	depends on BUSYBOX_CONFIG_FEATURE_EDITING
	help
	Enable history saving in shells.

config BUSYBOX_CONFIG_FEATURE_EDITING_SAVE_ON_EXIT
	bool "Save history on shell exit, not after every command"
	default BUSYBOX_DEFAULT_FEATURE_EDITING_SAVE_ON_EXIT
	depends on BUSYBOX_CONFIG_FEATURE_EDITING_SAVEHISTORY
	help
	Save history on shell exit, not after every command.
2 Likes

Is there a cheaper-easier-faster solution to enable command history than recompiling?

Can I substitute ash with bash and do that?

Also as I said in my original question, I'd like to keep history file to my external storage to preserve flash memory

I patch it to be in /tmp, but you can change the location as you want.
(Alternatively, busybox looks up HISTFILE variable that you can set.)

You can substitute ash with bash as your shell.
But as bash is then your default shell but is not included in the firmware image, you will be locked out of the router next time you use sysupgrade (as the new image will not have bash embedded.)

4 Likes

a few alternatives: use sudo, cmds are logged to main logfile or try script

1 Like

You can avoid this problem without the effort of recompiling by using the imagebuilder to create a custom sysupgrade image that includes the bash package.

create 2 users; login one with the ash shell and su - w bash; this eliminates the chance being locked out after upgrade, just a hint

2 Likes

i install bash and keep .bashrc and .bash_history between updates via /etc/sysupgrade.conf, ie

~# grep root /etc/sysupgrade.conf
/root/.screenrc
/root/.bash_history
/root/.bashrc
/root/.ssh/config

# cat .bashrc
# enable basic tab completion in openwrt
bind "set show-all-if-ambiguous on"; bind "set completion-ignore-case on"

# runtime limit of commands in history. default is 500!
export HISTSIZE="5000"
# ignoredups to make the searching more efficient
export HISTCONTROL="ignorespace:ignoredups"
# append, not owerwrite history file (should be set by default, just to make sure)
shopt -s histappend

alias ll='ls -lah --color=tty'

From here, every time you login, run bash and you're good