# How to make CLI command cache working? (busybox feature)

**URL:** <https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469>\
**Category:** Installing and Using OpenWrt\
**Created:** [June 19, 2017, 12:59pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469 "2017-06-19T12:59:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Koldur](https://forum.openwrt.org/letter_avatar/koldur/32/6_449c533f586939b6e5f246632a46421f.png) [@Koldur](https://forum.openwrt.org/u/Koldur)\
**Post date:** [June 19, 2017, 12:59pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/1 "2017-06-19T12:59:29Z")

</div>

In Arokh's OpenWRT FW when I used the up arrow, even after a restart of the router, the previous commands used in the CLI popped up. Now in a build I use here, it does not, I already installed BASH, but that doesn't seem to make a difference, anyone got any pointers on it?

I already tried searching on Google, but couldn't find anything useful, except for BASH and that doesn't work for me. Maybe I am not using the correct search string, but still, would love to get that function back.

---

<div class="post-metadata">

**Author:** ![hnyman](https://forum.openwrt.org/user_avatar/forum.openwrt.org/hnyman/32/19_2.png) [@hnyman](https://forum.openwrt.org/u/hnyman)\
**Post date:** [June 19, 2017, 2:57pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/2 "2017-06-19T14:57:17Z")

</div>

You need to re-compile busybox with the correct options.

I have done this in my own build, as I have wanted the command history to save between sessions while the router is running, but I am not writing the history to flash, so the history is only in RAM /tmp:

Manually set busybox options into .config:

```
CONFIG_BUSYBOX_CUSTOM=y
CONFIG_BUSYBOX_CONFIG_FEATURE_EDITING_SAVEHISTORY=y
CONFIG_BUSYBOX_CONFIG_FEATURE_EDITING_SAVE_ON_EXIT=y
```

(You can do that also in menuconfig)

Like said, I have patched busybox to avoid writing to flash. By default it would save to the file specified by HISTFILE env variable or secondarily to the root's HOME directory:

```
--- /dev/null
+++ b/package/utils/busybox/patches/310-save-history-in-tmp.patch
@@ -0,0 +1,12 @@
+--- a/shell/ash.c
++++ b/shell/ash.c
+@@ -13531,7 +13531,7 @@
+ if (iflag) {
+ const char *hp = lookupvar("HISTFILE");
+ if (!hp) {
+- hp = lookupvar("HOME");
++ hp = "/tmp";
+ if (hp) {
+ hp = concat_path_file(hp, ".ash_history");
+ setvar0("HISTFILE", hp);
+
```

If you want to save to flash, just toggle that config option in menuconfig, skip the patch and then define HISTFILE in your router user profile.

EDIT:  
added the comment that you also need to explicitly first say that you want to compile busybox with custom options. Devs want to prevent easy toggling of busybox options, so they are hidden behind a selection of going into custom...

---

<div class="post-metadata">

**Author:** ![tmomas](https://forum.openwrt.org/letter_avatar/tmomas/32/6_449c533f586939b6e5f246632a46421f.png) [@tmomas](https://forum.openwrt.org/u/tmomas)\
**Post date:** [June 19, 2017, 5:08pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/3 "2017-06-19T17:08:11Z")

</div>

Would that be worth writing it down in a howto, e.g. [https://wiki.openwrt.org/doc/howto/busybox.save.history](https://wiki.openwrt.org/doc/howto/busybox.save.history)?

---

<div class="post-metadata">

**Author:** ![golialive](https://forum.openwrt.org/user_avatar/forum.openwrt.org/golialive/32/816_2.png) [@golialive](https://forum.openwrt.org/u/golialive)\
**Post date:** [June 19, 2017, 7:50pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/4 "2017-06-19T19:50:25Z")

</div>

Hey there.

Bash comes with default bash history configuration. So maybe just install bash and use it instead of ash?

If you decide to point roots default shell in `/etc/passwd`, better make no mistake. Entering something invalid will prevent you from connecting by SSH and you'll need a serial interface to repair.

I just tested this in Virtualbox but I'd expect any regular router to be equally capable.

If you don't want to adjust `/etc/passwd` (which is dangerous, as I explained), you can just call ssh with bash as alternate login shell by `ssh root@hostname -t "/bin/bash -l"`.

Regards,  
Stephan.

---

<div class="post-metadata">

**Author:** ![Wolfeman0101](https://forum.openwrt.org/user_avatar/forum.openwrt.org/wolfeman0101/32/3009_2.png) [@Wolfeman0101](https://forum.openwrt.org/u/Wolfeman0101)\
**Post date:** [January 12, 2018, 11:29am UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/5 "2018-01-12T11:29:19Z")

</div>

I'd like my history to save across reboots but I'm totally lost what to do. Can you explain it a little more for idiots like me? Thanks!

---

<div class="post-metadata">

**Author:** ![escalade](https://forum.openwrt.org/user_avatar/forum.openwrt.org/escalade/32/3003_2.png) [@escalade](https://forum.openwrt.org/u/escalade)\
**Post date:** [January 12, 2018, 11:45am UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/6 "2018-01-12T11:45:51Z")

</div>

All you need is CONFIG\_BUSYBOX\_CONFIG\_FEATURE\_EDITING\_SAVEHISTORY, it'll save to /root/.ash\_history by default and persist through reboots. You could also make it persist through upgrades with this command before building:

`mkdir -p files/lib/upgrade/keep.d ; echo /root/.ash_history > files/lib/upgrade/keep.d/custom`

---

<div class="post-metadata">

**Author:** ![hnyman](https://forum.openwrt.org/user_avatar/forum.openwrt.org/hnyman/32/19_2.png) [@hnyman](https://forum.openwrt.org/u/hnyman)\
**Post date:** [January 12, 2018, 11:57am UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/7 "2018-01-12T11:57:44Z")

</div>

You just need to compile busybox with that option set.

---

<div class="post-metadata">

**Author:** ![Wolfeman0101](https://forum.openwrt.org/user_avatar/forum.openwrt.org/wolfeman0101/32/3009_2.png) [@Wolfeman0101](https://forum.openwrt.org/u/Wolfeman0101)\
**Post date:** [January 12, 2018, 12:43pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/8 "2018-01-12T12:43:43Z")

</div>

So this is the confusing part. How do I actually compile busybox?

---

<div class="post-metadata">

**Author:** ![hnyman](https://forum.openwrt.org/user_avatar/forum.openwrt.org/hnyman/32/19_2.png) [@hnyman](https://forum.openwrt.org/u/hnyman)\
**Post date:** [January 12, 2018, 12:46pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/9 "2018-01-12T12:46:59Z")

</div>

In the Openwrt/LEDE build system (runing on Linux)  
[https://lede-project.org/docs/guide-developer/build-system](https://lede-project.org/docs/guide-developer/build-system)

So, this is not something that you can change in a running system, but you really need to compile from source code.

---

<div class="post-metadata">

**Author:** ![Wolfeman0101](https://forum.openwrt.org/user_avatar/forum.openwrt.org/wolfeman0101/32/3009_2.png) [@Wolfeman0101](https://forum.openwrt.org/u/Wolfeman0101)\
**Post date:** [January 12, 2018, 12:48pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/10 "2018-01-12T12:48:28Z")

</div>

Ah ok that makes sense. Thanks for helping a newb.

---

<div class="post-metadata">

**Author:** ![anomeome](https://forum.openwrt.org/user_avatar/forum.openwrt.org/anomeome/32/15002_2.png) [@anomeome](https://forum.openwrt.org/u/anomeome)\
**Post date:** [January 12, 2018, 5:18pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/11 "2018-01-12T17:18:44Z")

</div>

may also want:

```
CONFIG_BUSYBOX_CONFIG_FEATURE_EDITING_SAVE_ON_EXIT=y
```

---

<div class="post-metadata">

**Author:** ![mccasian](https://forum.openwrt.org/user_avatar/forum.openwrt.org/mccasian/32/9918_2.png) [@mccasian](https://forum.openwrt.org/u/mccasian)\
**Post date:** [February 20, 2019, 11:06am UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/12 "2019-02-20T11:06:48Z")

</div>

the simplest solution that I found, is just don't using ash anymore but  
Switching to Bash / setting up .bashrc  
OpenWrt comes with Ash shell. If you would like to use Bash instead, here is how you can switch and add some nice colouring to it. Install Bash by issuing the following:

1 opkg update  
2 opkg install bash

Then edit /etc/passwd and change the root user line to this:

1 root:x:0:0:root:/root:/bin/ **bash**

After that, run this command (which will create /root/.bash\_profile and put “. $HOME/.bashrc” in it):

1 echo ". $HOME/.bashrc" \> /root/.bash\_profile

---

<div class="post-metadata">

**Author:** ![slh](https://forum.openwrt.org/user_avatar/forum.openwrt.org/slh/32/113591_2.png) [@slh](https://forum.openwrt.org/u/slh)\
**Post date:** [February 21, 2019, 12:31am UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/13 "2019-02-21T00:31:01Z")

</div>

I'd strongly recommend not to configure bash in /etc/passwd, but to enter it manually when connecting via ssh. The background for this advice are sysupgrades, bash is not part of the default firmware images - meaning it won't be preinstalled after a sysupgrade (unless you build OpenWrt with it included), preventing you from being able to log into your router (no valid shell, until you reinstall bash again).

---

<div class="post-metadata">

**Author:** ![ThomasCr](https://forum.openwrt.org/user_avatar/forum.openwrt.org/thomascr/32/4147_2.png) [@ThomasCr](https://forum.openwrt.org/u/ThomasCr)\
**Post date:** [May 31, 2019, 6:18pm UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/14 "2019-05-31T18:18:06Z")

</div>

fixed this by the following line in /etc/rc.local

`test -x /bin/bash && sed '/^root/ s_/bin/.*_/bin/bash_g' -i /etc/passwd || sed '/^root/ s_/bin/.*_/bin/ash_g' -i /etc/passwd`

when after a system upgrade bash is not available, it automatically switch back to ash, and also back to bash when it is installed 🙂

---

<div class="post-metadata">

**Author:** ![tmomas](https://forum.openwrt.org/letter_avatar/tmomas/32/6_449c533f586939b6e5f246632a46421f.png) [@tmomas](https://forum.openwrt.org/u/tmomas)\
**Post date:** [June 12, 2022, 10:38am UTC](https://forum.openwrt.org/t/how-to-make-cli-command-cache-working-busybox-feature/4469/15 "2022-06-12T10:38:30Z")

</div>

Closed on June 12, 2022, 10:38am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
