2

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.

  • 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. – MelBurslan Mar 23 '16 at 0:44
  • That sounds like a really great answer, @MelBurslan, and it's a nice idea to grep the env variable. If you can repost it as an answer I will mark it as solved tomorrow. – forgotstackxpassword Mar 23 '16 at 1:21
3

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

  • My pleasure.... – MelBurslan Mar 23 '16 at 4:15
3

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.

0

It's ugly, but I use this on older systems with only sh available like ESXi hosts:

cat `printf %s $HISTFILE`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.