Home  »  ArticlesGuidesHow ToTechnology   »   How to Clear Bash Command Line History in Linux

How to Clear Bash Command Line History in Linux

There is a bash or Unix shell history that logs all the commands that the user runs. These records are the basis of the command line history that can be accessed by scrolling back and forth through those records by using the “up arrow” or “down arrow” keys.

For very good reasons, it is possible to clear the entire command line history or delete selected entries.

The primary reason for clearing the contents of the Bash history is security. One good example is where you pass a password as a parameter when running a command like:

Using sshpass to set the password when copying files using SCP for example:

$ sshpass -p "password" scp -r [email protected]:/the/remote/path /the/local/path

This example is used to copy a file using curl.

$ curl --insecure --user username:password -T /the/sourcefile sftp://example.com/path/

This one is used to sign in to a MySQL database.

$ mysql -u root -p=password

Running any of those commands will end up having them being archived in the command line history. You would normally want to clear the history after running these types of commands to safeguard the passwords. This can be done by running:

$ history -c

What if you want to retain the entire history but get rid of those individual commands? Well, you would first need to get the line history by running:

$ history

This will display the entire bash command history. You then need to note the line number of the command you want to delete. When you are ready, type the following history command:

$ history -d 435

Where 435 is the line number of the command you want to get rid of. You would also want to take note that each user has only access to their own set of commands and not those typed in by other users. The root user can access other users’ bash commands.

That is the easiest way to either clear the command line history or delete individual commands by line number.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Available under:
Articles, Guides, How To, Technology