Categories
Design

How to use your bash history in the linux or macos terminal

Justin Pot has been writing about technology for over a decade, with work appearing in Digital Trends, The Next Web, Lifehacker, MakeUseOf, and the Zapier Blog. He also runs the Hillsboro Signal, a volunteer-driven local news outlet he founded. Read more.

How to use your bash history in the linux or macos terminal

Press the “up” arrow in the Mac or Linux command line and you’ll see the last command you ran. Keep pressing “up” and you’ll see more commands; you can go back days, months, or even years.

This is called your history, and it’s very convenient. If you made a mistake typing a long command, simply press “up” and fix the problem. If you want to re-connect to an SSH server you used the other day, simply press “up” until you see the relevant command.

It’s useful, but there’s also a potential security problem here, particularly if you accidentally typed a password in plain text at some point. How does one clear this history? Long story short, you can do so with two commands: history -c , followed by rm

/.bash_history . Here’s what those commands do, for greater clarity.

Clear the Current Session’s History

Your history can be broke down into two chunks. There’s your current sessions’ history, and there’s your long-term history. Our first command, history -c , deals with the current session.

How to use your bash history in the linux or macos terminal

The history command is built into Bash itself, and the -c modifier tells the program to clear that history. This command will prevent anything in your current session from being written to your long-term history, but does not clear out that long-term history.

Clear All of Your Bash History

If you want to remove the entirety of your history, run the following command:

How to use your bash history in the linux or macos terminal

If you don’t know, rm is a longstanding command for deleting files in UNIX-based systems.

/.bash_history is a simple text document, which stores you Bash history.

Alternatively, you could open the file and delete any lines you’re concerned about. On a Mac, type open

/.bash_history and your default text editor will open the file.

How to use your bash history in the linux or macos terminal

On Linux systems, replace open with the name of your preferred text editor, such as nano , vim , or gedit . One you’ve opened the file, you can delete any lines you’d rather not keep by hand. Save the file, then restart your shell, and the lines you’ve deleted will stop showing up.

Clear Your Terminal for a Like-New Session

This one is mostly unrelated, but I’m mentioning it anyway. The command clear makes your Terminal look like you just opened a new session, which is useful if you take a lot of screenshots and want things to look tidy (or don’t want people over your shoulder to see what commands you’ve been running.)

How to use your bash history in the linux or macos terminal

This is entirely aesthetic: scroll up and you’ll still see your previous output. But if you’re in my line of work, it comes in handy.

What is the shortcut to search my command history in macOS terminal?

For how long is the history available for searching? Where is it stored?

How to use your bash history in the linux or macos terminal

How to use your bash history in the linux or macos terminal

11 Answers 11

How about using Ctrl + R for searching on the Terminal Utility in Mac for searching on the command history,

Well for controlling how long the history would be retained that depends on a few shell environment variables, HISTFILESIZE which is nothing but number of lines of history you want to retain. Set a huge value for it in .bash_profile for it to take effect

How to use your bash history in the linux or macos terminal

Use Ctrl + R for searching a command from history in Terminal.

Type any substring of the command you want to search e.g. grep

It will return the latest command that matches your input. If that is not the command you were searching for, keep pressing Ctrl + R for next match until you find your command.

Once you found your command press Return to execute it.

If you want to exit without running any command, press Ctrl + G

PS: This answer is same as suggested by Inian, just giving more details for easy usage.

How to use your bash history in the linux or macos terminal

The command history is stored under your home folder in a hidden file called .bash_history. To view it’s content in nano, use the following command in Terminal:

Or open with your text editor (default is TextEdit):

In my case it’s a very long list and as I scroll through seems like the last

500 command is stored here.

/.history depending on your shell. – duplex143 Jun 10 at 12:40

Migrating an answer to SO from this answer on the Unix and Linux Stack Exchange:

Pressing ctrl + R will open the history-search-backward. Now start typing your command, this will give the first match. By pressing ctrl + R again (and again) you can cycle through the history.

If you like to be super lazy you can bind the up/down arrow keys to perform this search, I have the following in my .inputrc to bind the up/down arrow key to history-search-backward and history-search-forward :

Just type something (optional), then press up/down arrow key to search through history for commands that begin with what you typed.

To do this in .bashrc rather than .inputrc , you can use:

Use this command –

This works on both OSX and Linux.

History is stored in

/.history depending on your shell.

History is stored for 1000 or 2000 lines depending on your system.

/.zshrc for example in case of z shell. – manasshr Oct 26 ’20 at 20:47

For those who want to search specific command from history, you can do so with reverse-i-search . Reverse search allow you to type in any key words(any) that is part of the command you are looking for and reverse search navigate back to history, match previous commands incrementally and return the entire command.

It is especially useful as when one cannot remember all handy lengthy commands they use often. To do reverse-search ctrl + R and type any clue you have and that will return your previous commands matching the words you type. Then once found the command, hit Enter to execute it directly from search.

How to use your bash history in the linux or macos terminal

Since you mentioned viewing your history as a quick solution, via the Terminal.app. You might want to automate, or quickly view history, maybe from the dock. You may use the AppleScript application as one alternative. This is an optional approach to create a simple shortcut, as to many others.

  1. Open the AppleScript editor application.
  2. Add your specified commands, for history.
  3. Code
  1. Save as application, drag to dock for convenience.

History Storage & Time Stored Details

HISTSIZE Determines how many lines will be written to the history file.

HISTFILESIZE Determines how long the file.

Find out how long history is stored:

echo $HISTSIZE $HISTFILESIZE

Note: You may also increase your command history storage size in the length of two variables. You may achieve this through HISTSIZE and HISTFILESIZE environment variables which are located in your

It is possible to achieve this by modifying

/.bash_profile , the number placeholder with SIZE represent’s the number, lines value as example:

HISTFILESIZE will only set a maximum history value which is stored to the history file when a session is started. HISTSIZE will determine specifically how many lines will be stored or in other words, written at the end of the session. If the set HISTFILESIZE is determined to be a large value than what HISTSIZE is set, you will not view history larger than your set HISTSIZE . The reason is that the history file is overwritten with the HISTSIZE unless using histappend option turned ON.

You may use also histappend to append history, If the histappend shell option is turned on lines are appended to the history file. Otherwise, the overwritten alternative proceeds.

Modify history environment variables, set to a value:

Run the source command can be used to load any functions file into the current shell script or a command prompt.

How to use your bash history in the linux or macos terminal

Want to add a timestamp (date and time) next to each command from your Bash history? This can be done by using the HISTTIMEFORMAT Bash variable.

Bash keeps a history of the commands you type, which can be accessed by typing history . By default you see a number followed by the commands you’ve used recently:

With the help of the HISTTIMEFORMAT Bash variable you can show the date and time when each command was executed. This can be useful in various occasions, including to remember which commands you ran in a specific time-frame, to undo various operations, and so on.

It’s worth noting that if this variable is set, the time stamps are written to the history file, so they are preserved across shell sessions. So the first time you enable it, you won’t see the correct date and time for your previously used commands.

Another Bash history enhancement you might like: HSTR Makes Searching Your Bash Or Zsh Command History Easy

Set the Bash history to show a timestamp for your command history (for the current terminal session only) by using this command:

This command is only for this session, so you can see how it looks and optionally configure the date and time format (see below).

Now type history and you should see timestamp for your Bash history commands:

We’ve exported HISTTIMEFORMAT with the following timestamp:

  • %F : full date (year-month-date)
  • %T : time (hour:minutes:seconds)

There is a space after %T and before ” , so that there’s a space in your Bash history between the time the command was executed and the command itself. Without it, the two wouldn’t be separated.

You can customize the date and time. Use HISTTIMEFORMAT=”%Y/%m/%d %T ” to show the date as year(4 digits)-month-day, and time as hour(00..24):minutes:seconds. Show the date as month/day/year(2 digits) with %m/%d/%y . Want to show the time as 00..12 followed by AM / PM, instead of 00..24? Use %I:%M:%S %p as the time format. See the date command for how to further format the date and time.

It’s now time to export HISTTIMEFORMAT from your

/.bashrc file to make it default for all new terminal sessions for your user. You can either open your

/.bashrc file with a text editor and paste export HISTTIMEFORMAT=”%F %T ” (or some other date and time format) at the bottom, then save the file, or you can run this command to write it to your

Run this command only once, because it adds export HISTTIMEFORMAT=”%F %T ” to

/.bashrc each time you run it.

After this, all that’s left is to source the

/.bashrc file so the current terminal session uses the new settings (or you can open a new terminal and it will automatically pick up the new HISTTIMEFORMAT settings):

Remember: the first time you enable timestamps for your Bash history, you won’t see any date / time for your previously used commands. Only commands executed after you enable timestamps for your Bash history will show a date and time at the beginning of the line.

How to use your bash history in the linux or macos terminal

Want to add a timestamp (date and time) next to each command from your Bash history? This can be done by using the HISTTIMEFORMAT Bash variable.

Bash keeps a history of the commands you type, which can be accessed by typing history . By default you see a number followed by the commands you’ve used recently:

With the help of the HISTTIMEFORMAT Bash variable you can show the date and time when each command was executed. This can be useful in various occasions, including to remember which commands you ran in a specific time-frame, to undo various operations, and so on.

It’s worth noting that if this variable is set, the time stamps are written to the history file, so they are preserved across shell sessions. So the first time you enable it, you won’t see the correct date and time for your previously used commands.

Another Bash history enhancement you might like: HSTR Makes Searching Your Bash Or Zsh Command History Easy

Set the Bash history to show a timestamp for your command history (for the current terminal session only) by using this command:

This command is only for this session, so you can see how it looks and optionally configure the date and time format (see below).

Now type history and you should see timestamp for your Bash history commands:

We’ve exported HISTTIMEFORMAT with the following timestamp:

  • %F : full date (year-month-date)
  • %T : time (hour:minutes:seconds)

There is a space after %T and before ” , so that there’s a space in your Bash history between the time the command was executed and the command itself. Without it, the two wouldn’t be separated.

You can customize the date and time. Use HISTTIMEFORMAT=”%Y/%m/%d %T ” to show the date as year(4 digits)-month-day, and time as hour(00..24):minutes:seconds. Show the date as month/day/year(2 digits) with %m/%d/%y . Want to show the time as 00..12 followed by AM / PM, instead of 00..24? Use %I:%M:%S %p as the time format. See the date command for how to further format the date and time.

It’s now time to export HISTTIMEFORMAT from your

/.bashrc file to make it default for all new terminal sessions for your user. You can either open your

/.bashrc file with a text editor and paste export HISTTIMEFORMAT=”%F %T ” (or some other date and time format) at the bottom, then save the file, or you can run this command to write it to your

Run this command only once, because it adds export HISTTIMEFORMAT=”%F %T ” to

/.bashrc each time you run it.

After this, all that’s left is to source the

/.bashrc file so the current terminal session uses the new settings (or you can open a new terminal and it will automatically pick up the new HISTTIMEFORMAT settings):

Remember: the first time you enable timestamps for your Bash history, you won’t see any date / time for your previously used commands. Only commands executed after you enable timestamps for your Bash history will show a date and time at the beginning of the line.

Effectively using bash history will save you plenty of time in the Linux terminal.

How to use your bash history in the linux or macos terminal

Table of Contents

Whether you are a bash beginner or expert, you cannot go on working in the command line without using the super useful bash history feature.

You probably are already aware that if you use the up or down arrow keys in Linux terminal, you can go through the commands you had run earlier.

This is available thanks to the bash history command.

1. View your bash history

The simplest way to see the commands you had typed earlier is to use the command history.

It will show a hundred or perhaps a thousand commands stored in your history. The size depends on HISTSIZE variable.

How to use your bash history in the linux or macos terminal

If you don’t want your screen cluttered, you can limit the number of lines it displays. For example, to show only the last 7 commands from the history, use it like this:

How to use your bash history in the linux or macos terminal

2. Run command from bash history

When you are looking at the history, you can run a command by using its number with ! , like this:

How to use your bash history in the linux or macos terminal

You can rerun the last command with !! . A good use of this comes when you forget to use with a command and then quickly use it like this to run with sudo:

How to use your bash history in the linux or macos terminal

You may also run the last command starting with a certain text like this:

Suppose you ran echo $HISTCONTROL earlier and you want to run it again. You can use it like this:

And the above command will run the last ran echo command from your history.

How to use your bash history in the linux or macos terminal

There is a better option to search through the history, and I’ll show it you in the next section.

Bonus Tip: If you are not sure of the command, instead of running it, print it by adding :p to the end of it. So you use it like !echo:p , !23:p etc.

3. Searching through bash history

You may think that it is easier to use grep command for searching something through history like this:

A better and super handy way to search through history is to use the ctrl+r keys to start the reverse search and type the string you are looking for.

You can cycle through the choices by pressing ctrl+r repeatedly and when you find the command you were looking for, press ctrl+o or simply enter key to select and run that command.

For example, the example below searches for a command that had ‘aud’ in it.

How to use your bash history in the linux or macos terminal

If you don’t find the command, change your search string or press ctrl+g to come out of the reverse search.

4. Reuse arguments from previous commands in your history

Here is another cool stuff you can do with bash history. You can use arguments from previous commands in a new command.

You can use the last argument of the previous command like this:

For example, say you were reading a file with the less command and you decided you need to edit this file. Instead of typing it entirely, you use the !$ shortcut.

How to use your bash history in the linux or macos terminal

The above option gives you only the last argument of the previous command. If you want all the arguments, use this:

5. Cleaning your history of redundant entries

There is an environment variable called HISTCONTROL that help you with redundant entries in bash history. You can assign one of the three values to it:

  • ignorespace: With this variable set, you can run a command by putting a space before it. The command runs as usual but won’t be included in the history.
  • ignoredups: If there are two are more identical commands ran consecutively, only one of them will be recorded in the history.
  • ignoreboth: Sets both of the above mentioned features.

You should set the variable in your bashrc file so that it is set every time you use bash shell.

Too many commands in your bash history? You can clear with the option -c . This will clear the present history of bash and start adding commands afresh from now onwards.

How to use your bash history in the linux or macos terminal

There is a lot more to bash history than these five tips I discussed here. You can always refer to the man page of the history command for additional info. You may also want to learn about history builtins, if it interests you.

I hope you find this article helpful. If you have a favorite bash history feature, do share it in the comments.

When you’re running tons of commands every single day, it’s obvious that you may want to keep a track of your terminal activities. For advanced Linux users, this is the burning truth. Well, if your terminal is set to remember history, then checking out the entire history shouldn’t be a problem at all.

Need to find out the history with date and time? Let’s get started!

At first, let’s make sure that the time format of the history is set. For that purpose, we have to define an environmental variable “HISTTIMEFORMAT”.

Run the following command –

Let’s check out what each of the parts means.

  • %m – Month
  • %d – Day of the month
  • %y – Year
  • %T – Time of the command running

After setting the variable, it’s time to check out the history. Run the following command –

Voila! You can find out the entire history of all the commands you ran previously.

Run command from history

run the command again 1065 (sudo localctl set-local LANG=en_US.UTF-8)

Control the total number of lines in the history using HISTSIZE

Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 1000 commands will be stored in the bash history.

Clear all the previous history using option -c

if you want to clear all the previous history, but want to keep the history moving forward.

The command “history” is also highly customizable with various available parameters.

Check out all the available “history” features –

You can also export the man page to a text file for later reading.

Chris Hoffman is Editor-in-Chief of How-To Geek. He’s written about technology for over a decade and was a PCWorld columnist for two years. Chris has written for The New York Times, been interviewed as a technology expert on TV stations like Miami’s NBC 6, and had his work covered by news outlets like the BBC. Since 2011, Chris has written over 2,000 articles that have been read nearly one billion times—and that’s just here at How-To Geek. Read more.

How to use your bash history in the linux or macos terminal

With macOS Catalina, Apple is now using Zsh as the default shell. We love Zsh, but the trusty old Bash shell is still included with macOS, and you can quickly switch back to Bash if you prefer.

Zsh is only the default shell on newly created user accounts, so any existing accounts you have on an upgraded Mac will still use Bash by default unless you change it. Each user account has its own default shell preference.

From the Terminal

To change a user account’s default shell on macOS, simply run the chsh -s (change shell) command in a Terminal window.

Change the default shell to Bash by running the following command:

You’ll have to enter your user account’s password. Finally, close the Terminal window and reopen it. You’ll be using Bash instead of Zsh.

Change the default shell back to Zsh by running this command:

Enter your password when prompted. After you close the terminal window and reopen it, you’ll be using Zsh.

How to use your bash history in the linux or macos terminal

You can see a list of included shells you can select by running the following command:

How to use your bash history in the linux or macos terminal

From System Preferences

You can also change this option graphically from System Preferences if you prefer.

Head to System Preferences > Users & Groups on your Mac. Click the lock icon and enter your password. Hold the Ctrl key, click your user account’s name in the left pane, and select “Advanced Options.”

How to use your bash history in the linux or macos terminal

Click the “Login Shell” dropdown box and select “/bin/bash” to use Bash as your default shell or “/bin/zsh” to use Zsh as your default shell. Click “OK” to save your changes.

How to use your bash history in the linux or macos terminal

Bash on macOS Is Still Outdated

Note that the version of Bash (Bourne Again SHell) included with macOS is still pretty outdated, however. If you run bash –version , you’ll see that Catalina includes Bash 3.2.57 when Bash 5.0 is the latest version. Newer versions are licensed under the GPLv3 license, while Apple still distributes a version licensed under GPLv2.

In contrast, the version of Zsh (Z shell) included with macOS (check with zsh –version ), is Zsh 5.7.2, which is the latest version at the time of Catalina’s release.

How to use your bash history in the linux or macos terminal

If you want the latest version of Bash, you can install it yourself via Homebrew.

How to use your bash history in the linux or macos terminal

Description

This course is a complete journey on concepts, theory, and practice of Operating Systems with a focus on the Linux command-line and its tools.

We will learn, from scratch, how the Linux OS works and discover how the Linux shell works.

This course started as a gentle introduction to the theory of operating systems and evolved to become a complete masterclass on the Linux command-line and the concepts that underpin modern operating systems.

Just a heads up that this course is mostly aimed at programmers that want to learn about the command line, and it’s not aimed at networking or infrastructure professionals.

  • Operating System concepts (processes, memory management, scheduling, system calls, and kernel layers).
  • The history and evolution of popular operating systems, with a focus on UNIX and Linux.
  • The Linux command-line shell and popular utilities.
  • Files, directories, filesystems.
  • System administration and automation utilities.
  • Bash shell scripting.
  • Text editors for Linux.
  • Popular programming tools for Linux (gcc, make, autoconf, etc.)

This is a great course if you never touched Linux before and want to discover the motivation behind using Linux as an operating system to enhance your skills as a programmer.

We will use a Debian-based distro to practice and show examples in practice, but everything we learn will be valuable for any UNIX, Linux, or macOS terminal environment.

If this sounds like a fun journey to you… join us!

Is there a way to save all my typed terminal commands and view it like history in a log book?

How to use your bash history in the linux or macos terminal

5 Answers 5

This is automatically done. Bash stores your commands in

/.bash_history . If you want to have a look at the history, either print the output of this file using one of

Or you can use bash’s builtin command:

To clear the history, delete the file and clear the temp history:

The history size defaults to 500 commands. You can, however, increase this by adding a line to your

/.bashrc file to set the HISTSIZE variable:

This will not take effect immediately, but only to newly started sessions. To apply this, re-source the .bashrc file:

or run HISTSIZE=. in your current session.

How to use your bash history in the linux or macos terminal

How to use your bash history in the linux or macos terminal

You can type history on a terminal to view all the previous executed commands.

You can truncate the output to some lines (where 5 is the number of lines):

If do you want to view only commands containing a string (i.e. mv ), you can do this:

You can recall a command by typing ! followed by the entry number.

Let’s say that I have a history like this:

  • To run mkdir foo , you can type !2 .
  • To run the last command, you can use !-1 or !!
  • To run the penultimate, you can use !-2

If you run a command that fails because it needs root privileges (i.e. touch /etc/foo ), you can use sudo !! to run the last command as root.

  • If you type !man you will execute the last command that begins with man
  • If do you type !?man? it will execute the last command that contains man (not neccessarily at the line begin)

If do you have a typo in a command, you can fix it this way. Let’s say that I type cat .bash_hi , to replace .bash_hi by .bash_history I only need to type ^hi^history^ .