27

Where does the bash shell store the actual terminal session? I want to read the output of commands I used before. All I can find googling is how to store the output of a command.

Since output is displayed on the screen, it has to be stored somewhere anyway. So my question is: where?

5 Answers 5

24

Bash only stores history of the commands you ran (which you can retrieve by typing history). Unless you already have set the scroll-back to a very high number, there is no way to see the outputs that are older than the set value of scroll-back. Also setting this value to a very high number will make your scrolling sluggish since the lines are stored in the memory.

To store your future commands and their outputs, there are few options:

Using screen

Start a screen session by entering screen. Once you are inside screen, press Ctrl-a, then :, then enter log. All the I/O will be captured in screenlog files in the directory where you started the screen command.

Using script

You can start by typing script. A script session will start that will capture all the I/O to a file named typescript. You can exit the script session by Ctrl-d and view the logs in the typescript file.

Using tee

tee is a handy tool. You can do something like this:

$ bash | tee log.txt

This will open a new bash shell inside the one you are already running. When you exit out of this, you can see the outputs in the file called log.txt

Other ways

As Dustin Kirkland suggested in this post, you can also use byobu. Although, I have never used, terminal screencasting tools such as Shelr also sounds like an option.

1
  • Wow! Sorry for a non-constructive comment, but I have to say (write) it: this answer is just amazing.
    – Andyc
    Jul 20, 2021 at 6:01
8

There are lots of questions related to this one, but they all start with the (right) assumption that the output isn't logged.

So my question is: where?

Nowhere: Bash's (and any other shell that I know of's) output isn't logged.

Since output is displayed on the screen, it has to be stored somewhere anyway.

It is, but most likely not in plain text and not even on the disk (although this really is up to the terminal emulator): most likely, and at least in most cases, in a memory segment allocated by the terminal emulator.

However for sure it isn't logged to a file, at least not by the shell, and I don't know of terminal emulators which log the output to a file by default.

Indeed this doesn't mean it's impossible to log the terminal output: first I'll mention something that I think not many are aware of, since I've never saw anyone mentioning this at least here on Ask Ubuntu: Konsole allows to save the scrollback to a file (perhaps there are other terminal emulators that provide such a functionality, Konsole is just the only one I know of), although this is limited by Konsole's scrollback size limit.

This is often not really useful though, and most likely you'll want to look into "proper" solutions to log a whole session's output to a file (How do I log all input and output in a terminal session? and Ron's answer).

1

There is a command named as script , if not installed then apt-get install script would do it.

Then in terminal just type script.

and do what ever you want , after you are done just type exit and then there will be a file created at your current directory with all stdout and stdin information.

hope it helps you.

1
  • Package is NA in ubuntu 18.04
    – markroxor
    Jan 3, 2020 at 9:21
1

This has nothing to do with your shell (bash), it isn a feature of the terminal emulator you are using. It is stored in your terminal's "scrollback buffer". I haven't been able to find any clear explanations of where exactly this is stored, but personal experience has shown me that it is stored somewhere in /tmp.

As the answer of the question linked to above suggests, it is most likely stored in a nameless file. To see what I mean, open a new file with a text editor:

gedit newfile

Write a line of text to the file and save it. Now, while that file is still open, open a terminal and delete it:

rm newfile

Since you still have the file open in gedit, you can continue writing to it. You could even write several gigabytes of data into it, despite the fact that the file has been deleted. That's because deleting a file simply removes the link pointing to its inode. If its file descriptor is held open by another program, data can be written to it, despitre the fact that there is no longer an actual link (file) corresponding to the file descriptor on the file system.

All this is to say that your terminal's history is probably saved using a trick like that, somewhere in a deleted file in /tmp. What you probably actually want is to simply increase the scrollback buffer size of youre terminal emulator so you can just scroll up and see it. The details of how to do this depend ont he terminal emulator you are using. Most have a command line switch that lets yous et this and many also have a GUI way of setting it. For example, in gnome-terminal (the default on Ubuntu) it's Edit -> Preferences -> Profiles --- click on your profile --> Edit -> Scrolling -> Limit scrollback to NNN:

gnome-terminal options screen

3
  • Interesting insight. Inspecting a longer history in the terminal is not what i want though: I want to have access to the cout to the terminal i.e. all that has been written there FROM E.G. PYTHON Oct 29, 2018 at 9:47
  • @DanielBöckenhoff then you probably want script as described in the accepted answer.
    – terdon
    Oct 29, 2018 at 12:48
  • Sure. I accepted it :) Nov 5, 2018 at 13:17
-2

Terminal session is stored in .bash_history file. Enter this in your terminal echo $HISTFILE, this would give you the path of .bash_history file.

1
  • 3
    This gives only the previously run commands, not the output of the commands.
    – Ron
    Apr 19, 2016 at 9:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .