Last Updated on September 24, 2020 by Christopher G Mendla
I was working in Ubuntu terminals both locally and on a Digital Ocean Droplet. I wanted to find a command that I used previously but it was not saved in the terminal history. This is a design feature but can be overridden.
The terminal history can be very useful when you want to redo a complex command that you know you used recently. You can up arrow to go through the previous commands or type “history” and your recent commands will be similar to the following but with a longer list of recently used commands.
353 git checkout add_rubocop_and_run
354 git checkout -b add_rubocop_and_run
355 gem install rubocop
357 rubocop --auto-correct
358 rake test
I was working through installing a Rails server on a Digital Ocean Droplet. I found a command to start the unicorn server, with my configuration file, in the Production environment:
sudo unicorn -c config/unicorn.rb -E production -D
A little while later, I wanted to rerun that command. I up arrowed and it wasn’t there. I did a ‘history’ command and it wasn’t showing there either.
After a little digging, I found out why the commands were missing. More recent versions of Ubuntu will not save the command if it is preceded by a space. In other words, if you copy and paste a command but caught a leading space, the command will not be entered in the command history.
This is by design and makes sense. If you are aware of this (and many people aren’t) you can start a command with a space if you are entering sensitive information. For example, if you are using a command that passes credentials or perhaps working with an encryption key.
HISTCONTROL A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. https://www.mediacollege.com/cgi-bin/man/page.cgi?topic=bash
To determine your current settings, use
The settings that can be returned are explained below
There are a number of ways to approach this:
This is the riskiest option. If you feel that your machines are secure enough, you can turn off the leading space feature and all entries will be saved in your history. You can do this with
Keep in mind that ALL commands are now saved including any commands with sensitive information
You can change HISTCONTROL with the export command as above with the following parameters
You would need to keep this in the back of your head as you are working. If you are pasting a command that you think you would like to be available in the history, you need to check to make sure you aren’t including a leading space.
The command history can be a great timesaver. However, it can expose sensitive information. Decide how much risk you can accept vs. how much productivity you can gain from the availability of all commands in your bash history.
This behavior will also apply when you run a terminal in VS Code. I’m assuming that it could also apply to machines that run Mac OS as well.
Your email address will not be published. Required fields are marked *
Save my name, email, and website in this browser for the next time I comment.