How can I access the history buffer in sh? (not in Bash)

10,766

Solution 1

unless it is explicitly set, there is no default shell history. to verify if you have it or not, the command env|grep -i hist can be executed and if you see variables like histfile etc. (may be capitalized) it means that your shell history is enabled, and you should see where the history file is located. To clear, you can do any decent method, other than deleting the file

Solution 2

Not all shells support history. dash has limited history support through the fc command if compiled with libedit support, but does not support it otherwise. dash is not capable of saving a history file.

Often, if a shell doesn't support the up arrow key, you can assume that it won't save a history file either. This isn't a hard-and-fast rule, but it's true in every case I've come across.

Some other shells (bash notably, but others too) use readline, which generally saves history under your home directory as a ~/.*_history file, with * being replaced with the name of the program. Python 3 uses readline for example, and stores its history in ~/.python_history.

Depending on the shell, the save location may or may not be configurable. bash and zsh use the HISTFILE (all caps) variable. If your shell is configured correctly this should not be an environment variable, so if you see it in env you have made a mistake (but it should still show in the output of set or printf %s\\n "$HISTFILE").

If a program doesn't support history (for example, ed, shell scripts using read, or older versions of mysql), there is a cross-platform application called rlwrap which will use readline to give it bash-like tab completion, arrow key handling, and history.

Share:
10,766

Related videos on Youtube

forgotstackxpassword
Author by

forgotstackxpassword

apples by day, wannaBSD ninja; Be addicted to ~ learning

Updated on September 18, 2022

Comments

  • forgotstackxpassword
    forgotstackxpassword over 1 year

    Is there a command history in sh, for example how to access it, and as well, how to ensure it is clear?

    To clarify shells here: let's say you've logged on to a non-Gui Linux system, and $SHELL is not set to Bash, but rather in Dash or whatever Bourne-like shell that may be.

    Man pages referred to a "history" command, which I've seen in some BSD systems, but this was not actually in my shell, and I couldn't easily find the answer in the subsequent sh man and info pages.

    decriptor's comment in this post got me thinking maybe it is more to do with needing to learn about the key bindings in sh rather than actually something like the differences between the shells.

    For future readers, here is a great post on shell history.