Currently i need to capture ALL the output of my terminal (bash) for later review. Right now i have the following scripted at the last line of my .bashrc file:

. /home/[USER]/.bash_profile

Hereby i source my .bash_profile command; if i leave this command out, my .bash_profile will not be executed at all.

My .bash_profile script has the following content:

export HISTSIZE=999999999
export SYNCPATH="/tmp" 
export HISTFILE=$SYNCPATH/bash_history_$(date +%d-%m-%Y)
shopt -s histappend                      # append to history, don't overwrite it
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

script -m advanced -a -f -T $SYNCPATH/timeinformation_$(date +%d-%m-%Y) $SYNCPATH/typescript_$(date +%d-%m-%Y)

This works HOWEVER it reexecuting the script command (looks like it is stuck in some kind of loop). See screenshot and it keeps spawning new script processes.


I can quit this by give the command CTRL+D but then the output will not be saved.

Anyone has some tips?


script forks a subshell, so generally it is possible that such scripts are sourced.

However the subshell is no login shell, hence ~/.bash_profile should not be sourced, but only ~/.bashrc instead.

I verified this on my Raspbian Bullseye system, where it seems to work like you want it:

  • On initial login ~/.bash_profile is sourced
  • which calls script
  • which forks a new non-login bash
  • which calls ~/.bashrc only
  • hence no nested script instance is created.

I'm not sure now why in your case it seems to be the other way round, so that on initial login you need to source ~/.bash_profile manually (or do you open a terminal emulator or other kind of non-login shell?), while the subshell sources ~/.bash_profile automatically instead.

But you can prevent nested script executions easily:

pgrep script &> /dev/null && return
  • Within a sourced script and outside of a function, return stops further sourcing, hence it is similar to calling exit within an executed script.

However it makes sense to track down which script is sourced in which case on your system and prevent this unintended case in the first place by placing the script call into the correct script.

Further information about which files are sourced in which order by which types if bash invocations (login, interactive, non-interactive) and how a certain type can be forced and derived from within the shell, read man: https://linux.die.net/man/1/bash Scroll a bid down to "Invocation" :)

| improve this answer | |
  • Thanks @MichaIng; i noticed that only .bashrc is invoked when i start a new terminal (inside X). However when i only put the code mentioned inside the .bashrc it keeps on repeating the script (hence the screenshot). I removed the .bash_profile, .xsessionrc, .profile from my homedirectory and i put a temporary variabele in .bashrc so that i know this file is sourced when i open a terminal. – Dave - Jun 6 at 20:16
  • for now i have it fixed by adding the following to the header of the script. naam_script='ps -p $PPID -o comm=' naam_script_command="script" if [ "$naam_script" = "$naam_script_command" ]; then echo "Je bent aangeroepen door script; alles wordt nu gelogd!" else I check if my parent was the command ' script', if that's the case i don't do anything (except printing a message). With this solution i don't get the loop; however i still find it strange – Dave - Jun 6 at 21:09
  • When you open a terminal emulator inside X, it is expected that only ~/.bashrc is sourced, since it is not a login shell then. Note that a login shell obviously means that you needed to login to the terminal, like when you boot into console or SSH into the machine. So it also is expected that it loops with the script execution in ~/.bashrc, since script forks a new non-login shell as well. You could put some echo into ~/.bash_profile now to verify that it is not sourced when script is called. And you can test a login shell via sudo login to check if ~/.bash_profile is sourced – MichaIng Jun 6 at 23:03
  • I added the bash man page to the answer, which explains very well which script is sourced in which case. – MichaIng Jun 7 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.