TechBuzzPro

Bash History Length: How to Change or Disable It

· 5 min read · Apps & Browsers

In this article
  1. How Bash History Works
  2. Changing History Length
  3. Disabling Bash History Completely
  4. Choosing the Right Approach
  5. Common Problems and Fixes
  6. Alternatives and Advanced Tips
  7. Frequently Asked Questions

To change the length of your bash command history, set the HISTSIZE and HISTFILESIZE variables in your ~/.bashrc file; to disable history completely, set HISTSIZE=0 and HISTFILESIZE=0. These simple changes give you full control over how many commands bash remembers, both in the current session and across reboots.

How Bash History Works

Bash maintains two separate history stores. The first is the in-memory history list for the current shell session, controlled by HISTSIZE. The second is the history file (usually ~/.bash_history) where commands are saved when the session ends, controlled by HISTFILESIZE.

When you type a command, bash adds it to the in-memory list. When you exit the shell, bash writes that list to the history file, appending to whatever was already there. On the next startup, bash reads the file back into memory. The two variables can be set independently, so you could keep a large file but only show a smaller number in the current session, or vice versa.

Changing History Length

To adjust how many commands bash remembers, edit ~/.bashrc (or ~/.bash_profile on macOS) and add or modify these lines:

HISTSIZE=1000
HISTFILESIZE=2000

Here’s what each variable does:

  • HISTSIZE – the maximum number of commands kept in memory for the current session. This is what you see when you type history.
  • HISTFILESIZE – the maximum number of lines in the history file. This controls how many commands persist across sessions.

After editing, run source ~/.bashrc or open a new terminal to apply the changes. Note that changing HISTSIZE does not truncate the existing in-memory list immediately; it only affects new commands added after the change.

Disabling Bash History Completely

To turn off history entirely, set both variables to zero:

HISTSIZE=0
HISTFILESIZE=0

With these settings, bash will not store any commands in memory, and the history file will be emptied (or not written to) when you exit. However, there are a few caveats:

  • Commands from the current session are still available until you exit, even with HISTSIZE=0? Actually, with HISTSIZE=0, no commands are added to the in-memory list, so history will show nothing new.
  • If HISTFILESIZE=0, the history file is truncated to zero bytes when the shell exits. This is a good way to clear existing history.
  • Some distributions set HISTFILESIZE to a large number by default, so be sure to override both.

Alternatively, you can use the unset HISTFILE command to stop bash from writing to the history file at all. This is useful for a single session without permanently changing your configuration.

Choosing the Right Approach

The best choice depends on your needs:

  • Keep history but limit size – set HISTSIZE=500 and HISTFILESIZE=1000 to retain a reasonable number of commands without cluttering your file.
  • Disable for privacy – set both to 0 or unset HISTFILE to leave no trace of your commands.
  • Disable only for certain sessions – run unset HISTFILE in a shell where you don’t want commands saved, while keeping your default settings.

You can also use HISTCONTROL to ignore duplicates or commands starting with a space. For example, HISTCONTROL=ignorespace prevents commands prefixed with a space from being saved, which is handy for sensitive commands like passwords.

Common Problems and Fixes

Here are a few issues you might run into:

  • History not saving after reboot – ensure HISTFILESIZE is not zero and that the history file is writable. Also check that HISTFILE points to the correct location.
  • Changes not taking effect – remember to source your .bashrc or start a new terminal. Some systems use .bash_profile for login shells; edit that too if needed.
  • History file grows too large – set a reasonable HISTFILESIZE and consider using HISTCONTROL=erasedups to remove duplicate lines.
  • Commands missing from history – if you use multiple terminals, each session writes its own history on exit, and the last one wins. To avoid losing commands, you can enable shopt -s histappend to append instead of overwrite.

Alternatives and Advanced Tips

If you want finer control, consider these options:

  • Per-command exclusion – prefix a command with a space (if HISTCONTROL=ignorespace) to keep it out of history.
  • Clear history on exit – add history -c to your .bash_logout file to wipe the in-memory list when you log out.
  • Use a different history file – set HISTFILE=/path/to/other_file to keep separate histories for different projects or environments.
  • Timestamp your history – set HISTTIMEFORMAT="%F %T " to see when each command was run.

These advanced options give you granular control over what is recorded and how it is stored.

Frequently Asked Questions

Does setting HISTSIZE to 0 clear my existing history?

No. Setting HISTSIZE=0 stops new commands from being added, but the current in-memory list remains until you exit. To clear it immediately, run history -c. The history file is only truncated when HISTFILESIZE=0 and you exit the shell.

What's the difference between HISTSIZE and HISTFILESIZE?

HISTSIZE controls how many commands are kept in memory during the session, while HISTFILESIZE controls how many lines are stored in the history file. The file size can be larger than the memory size, allowing you to keep a longer record than you see in the current session.

Can I disable history for just one command?

Yes. If you set HISTCONTROL=ignorespace, any command preceded by a space will not be saved to history. For example, secret-command (with a leading space) will be omitted.

Why is my history file not growing even though I set HISTSIZE to a large number?

Check HISTFILESIZE – it may be set to a lower value, which caps the file size. Also ensure HISTFILE is set to a writable path. If you use multiple terminals, the last one to exit overwrites the file unless you enable shopt -s histappend.

Is it safe to delete the .bash_history file?

Yes. Deleting ~/.bash_history simply removes your command history. Bash will recreate the file when you exit a session. If you want to stop it from being recreated, set HISTFILESIZE=0 or unset HISTFILE.