Bash History

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,489
Reaction score
3,222
Credits
31,259

Understanding and Managing Bash History in Linux​

The Bash shell in Linux keeps a record of the commands you execute, which can be incredibly useful for recalling and reusing previous commands. This history is stored in a file called ~/.bash_history. Let's explore how to manage this history effectively and use some powerful features to enhance your command-line productivity.

Configuring HISTSIZE and HISTFILESIZE​

Two important environment variables control the behavior of your Bash history:

  • HISTSIZE: This variable sets the number of commands to remember in the command history for the current session.
  • HISTFILESIZE: This variable sets the maximum number of lines contained in the history file.
You can set these variables in your ~/.profile or ~/.bashrc file. Here’s how you can do it:

In your ~/.profile or ~/.bashrc file add the following if it isn't already there.
Code:
HISTSIZE=1000
HISTFILESIZE=2000

Why ~/.bashrc might be better than ~/.profile:

  • ~/.bashrc: This file is executed for interactive non-login shells. It’s a good place to set environment variables that you want to be available in all your interactive shell sessions.
  • ~/.profile: This file is executed for login shells. It’s typically used for setting up environment variables that should be available in all shell sessions, including non-interactive ones.
For most users, setting HISTSIZE and HISTFILESIZE in ~/.bashrc is preferable because it ensures these settings are applied to all interactive shell sessions.

Using the ! and !! Operators​

Bash provides some handy shortcuts for reusing commands from your history:

  • !!: Repeats the last command.
  • !n: Repeats the command at position n in the history list.
  • !string: Repeats the most recent command that starts with string.
Examples:

Code:
 $ ls -l

To repeat the last command
$ !!
$ ls -l

To repeat a specific command from history
$ history
1 ls -l
2 cd /var/log
3 tail -f syslog
$ !2
$ cd /var/log

To repeat the last command starting with 'tail'
$ !tail
$ tail -f syslog

Searching Bash History with grep​

You can use the grep command to search through your Bash history for specific commands. This is particularly useful if you remember part of a command but not the entire thing.

Example:

Code:
 $ history | grep 'ssh'
45 ssh user@host
78 ssh -i ~/.ssh/id_rsa user@host


By configuring your Bash history settings and using these powerful features, you can significantly enhance your efficiency and productivity on the command line. Happy coding!
 
Last edited:


In ~/.profile or ~/.bashrc
HISTSIZE=1000
HISTFILESIZE=2000

~/.profile permission denied

sudo ~/.profile command not found

Unclear. How are they accessed in terminal ?

I moved on by accessing .bashrc manually, and I find that those settings re HIST are already in place.

Others may not have the same experience
 
In ~/.profile or ~/.bashrc
HISTSIZE=1000
HISTFILESIZE=2000

~/.profile permission denied

sudo ~/.profile command not found

This is a typo. I'm sorry.

This should say...

"In your ~/.profile or ~/.bashrc file,
add the following lines if they are not there".
 
I would use HISTORY and it's a great feature, but some admins seam to use HISTORY as documentation purposes mainly. So if they need to repeat some task, they would re-login as that user, on the correct server, and look at the commands run, to then redo them. A problem with that is that HISTORY does not say anything about correctness of the commands. It just lists what you typed. It may all be typos. All commands may fail, but you can't tell.

A problem with HISTORY is also that passwords can be stored in it, as HISTORY doesn't know which word is a password and which not.
As stated, because it's just a list of commands, nothing says any given command is good or not, so if you use it for documentation purposes, it's just a bad idea.

If you ever enter an a server that is unknown for you, grab the full history first thing after login.

I could state how to avoid commands being stored in servers with HISTORY enabled, but this thread is about positive using of this tool, we'll get that another time.
 
A problem with HISTORY is also that passwords can be stored in it,

That is a good point, I probably should have included that, but usually this is more because of user error than the actual commands. For example, scp, ssh, passwd, and most other commands hide the password from the terminal. Some commands like curl and wget do. But even so, I have accidentally typed a password on the command line. However, you can always change your password. The other good thing is that users can only see their own history, not someone else's history. Of course, root can, and anyone with sudo privileges can.

But also, users can usually delete their history file if they want to.
 
That is a good point, I probably should have included that, but usually this is more because of user error than the actual commands. For example, scp, ssh, passwd, and most other commands hide the password from the terminal. Some commands like curl and wget do. But even so, I have accidentally typed a password on the command line. However, you can always change your password. The other good thing is that users can only see their own history, not someone else's history. Of course, root can, and anyone with sudo privileges can.

But also, users can usually delete their history file if they want to.

If you look for your password in the history, don't forget to clean that command as well:

history | grep mysupersafepassword

:)


You can use any command with a variable for the password, if you think about it on time...
And also, don't set the variable this way :

mypassword=mysupersafepassword

You should set it with READ
 
Last edited:
If you have used some command that used sensitive information and you don't want to save your current session's history in your ~/.bash_history file you can set your HISTFILE shell variable using HISTFILE=/dev/null to prevent your current login session from saving your current session's history to your ~/.bash_history file for security reasons. I use an alias ush='HISTFILE=/dev/null' so I can type ush at a command prompt to ensure my history for that login session will not be saved.

Signed,

Matthew Campbell
 
Yes absolutely, but the point is that if you forgot to mask your password, you probably also forgot to disable history. That is, if you even know history is enabled, and even know what it is, and maybe even be allowed to change it, both in a technical manner and a permission-wise way.
 


Staff online

Members online


Top