September 23

0 comments

Ubuntu terminal history does not save commands that are preceded by a space.

By Christopher G Mendla

September 23, 2020


Last Updated on September 22, 2023 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.

Background.

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
  356  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.

The cause of the missing commands

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

Determining your settings.

To determine your current settings, use

echo $HISTCONTROL

The settings that can be returned are explained below

Solving the missing command issue.

There are a number of ways to approach this:

Toggling leading space feature.

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

export HISTCONTROL=""

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

  • ignoredups – This will ignore duplicate commands. NOTE THE SPELLING
  • ignorespace – This will ignore commands preceded by a space.
  • ignoreboth – Ignore both duplicates and preceding spaces.
  • “” – turn off all ignores

Make sure that you don’t include a leading space for commands you want to save

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.

Summary

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.

Notes

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.

Christopher G Mendla

About the author

A web developer living in Southampton, PA

Self motivated critical thinker and problem solver providing technology consulting services.

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}