1

I want to save my all terminal outputs. If I run "script" command from some terminal session it works fine. But if I put in .bashrc file, then .bashrc file keeps loading again and again

Script started, output log file is 'typescript'.
Script started, output log file is 'typescript'.
Script started, output log file is 'typescript'.
Script started, output log file is 'typescript'.

I tried this solution where command is skipped second time by setting local variable. https://unix.stackexchange.com/a/323920/381656

This also creates problem. If I run this command from simple terminal it works fine. But if I put in .bashrc then it always changes present directory into home directory. For example if I use split screen in terminator, then instead of opening in that directory it opens in home directory.

Is there a way to make .bashrc command run similarly as I run from active terminal session.

Update

After some hit and trial, I found out what was problem, why it was not opening in same directory.

Lets say if I open terminal in /abc/xyz. Now when script is executed then new shell is launched. If I change location of this shell it will not effect original terminal. Original terminal still holds location /abc/xyz. And if I try to split screen or open new tab, then parameter of current directory is taken from parent shell not from shell of script.

Parent shell's environment variable "PWD" needs to be updated as child shell for correct result. But as mentioned in this other question, it is not possible.

Is it possible to pass environment variables from child to parent in user space?

One possible hack would be using a text file for storing current directory, and while opening new tab updating to that location.

4
  • TERM=xterm-256color, SHELL=/bin/bash
    – Monu
    Dec 9, 2020 at 23:24
  • Oh, that is not what I meant, I meant terminal emulator, i.e. Gnome Terminal, Xterm, etc. If you run xterm -l, you will see that a Xterm file will popup in your home directory with the terminal contents.
    – Quasímodo
    Dec 9, 2020 at 23:27
  • Oh sorry, I use xfce, and have xfce4-terminal. I tried -l but this one does not have -l option.
    – Monu
    Dec 9, 2020 at 23:29
  • .bashrc is used in starting of terminal loading. Isn't there something which is executed after loading is complete with all paths and environment variables.
    – Monu
    Dec 9, 2020 at 23:32

1 Answer 1

0

The script command itself launches bash as a subprocess. That subprocess will launch script (because it is listed in .bashrc) and this will launch another bash process and so on and so on ... This is why you see it same text again and again. You need to a) make sure that script is only invoked once and b) parallel script invocations (i.e. multiple bash processes) don't overwrite each other's output. You could put the following at the very end of your .bashrc

# Assumes bash-4.2, see https://stackoverflow.com/a/17538964/2923406
[[ -v IN_SCRIPT ]] || { export IN_SCRIPT=1 ; script "$HOME/typescript.$(date --rfc-3339=ns | tr ' ' '_')" ; exit $?; }

This will invoke script only if the variable IN_SCRIPT is not present. IN_SCRIPT gets exported to make sure the subprocess doesn't start script again. Furthermore, all script processes write their output to a file named "typescript.". So you will end up with several files when you run several terminals in parallel.

6
  • Hello @Rolf, thanks for answer. But as you can see, I tried something like this (unix.stackexchange.com/a/323920/381656). If I run this command from simple terminal it works fine. But if I put in .bashrc then it always changes present directory into home directory. For example if I use split screen in terminator, then instead of opening in that directory it opens in home directory.
    – Monu
    Dec 15, 2020 at 16:22
  • And this is happening because .bashrc is always runs twice. And I think in process it losses arguments of current directory, passed while opening new tab.
    – Monu
    Dec 15, 2020 at 16:24
  • I see. I have put the line I wrote above at the every end of my .bashrc and it worked just fine. Since the script invocation stats itself a new bash subprocess the latter will just start start in ~; that's the default. You can probably tweak within terminator. You'll need to pass the cwd as argument somehow. There will probably be other issues like this when starting script from within your .bashrc.environment then.
    – Rolf
    Dec 16, 2020 at 13:18
  • Problem is not terminal specific. If I use xfce4-terminal instead of terminator, and try to open new tab in any directory, it always opens in home directory. And if I remove script line from .bashrc then it opens in that directory.
    – Monu
    Dec 16, 2020 at 16:38
  • Yes, I know. I said you might need to find a terminal-specific solution to that terminal-unspecific problem. It is complicated to have both a) run script that executes bash on startup and b) keep the cwd of the parent process.
    – Rolf
    Dec 17, 2020 at 8:25

You must log in to answer this question.

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