Apple Terminal startup errors (session save/restore)

Recently (possibly with the Sonoma update, but possibly even more recently than 14.0), Apple Terminal has been giving the following error on startup:

date: illegal time format
usage: date [-jnRu] [-I[date|hours|minutes|seconds]] [-f input_fmt]
            [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]
            [[[[mm]dd]HH]MM[[cc]yy][.SS] | new_date] [+output_fmt]

I have traced this to an error coming from /etc/bashrc_Apple_Terminal which is largely concerned with saving and restoring sessions for the various terminal tabs and windows.


It comes from the lines

    if [ -r "$SHELL_SESSION_FILE" ]; then
	. "$SHELL_SESSION_FILE"
	rm "$SHELL_SESSION_FILE"
    fi

This references $SHELL_SESSION_FILE which is written by the following lines:

	if [ -n "$SHELL_SESSION_FILE" ]; then
	    echo -ne '\nSaving session...' >&2
	    (umask 077; echo 'echo Restored session: "$(/bin/date -r '$(/bin/date +%s)')"' >| "$SHELL_SESSION_FILE")
	    declare -F shell_session_save_user_state >/dev/null && shell_session_save_user_state
	    shell_session_history_allowed && shell_session_save_history
	    echo 'completed.' >&2
	fi

This ends up being run on shell exit by `trap shell_session_update EXIT` (the function shell_session_update() runs another function which in turn contains these lines.)


The error occurs because $SHELL_SESSION_FILE incorrectly contains the following text:

Saving session...echo Restored session: "$(/bin/date -r 1698759014 Saving session...)"

rather than

echo Restored session: "$(/bin/date -r 1698759014)

For some reason, the `Saving session...` text — which is supposed to be sent to stderr — is appearing in the file (twice!). (And in fact I sometimes get other text incorrectly being added to this file from a `printf` line elsewhere in /etc/bashrc_Apple_Terminal.)


I think I didn't make any changes to cause this, but perhaps I am wrong. The directory `~/bash_sessions/` seems to have recent files with this error, and slightly older files (as recent as yesterday!) which don't. I regularly update various packages with homebrew, and I have recently installed Python 3.12 from python.org, but I can't see how they could reach into the guts of shell startup.


This error occurs even when I have no .bash* startup files; in fact, I see this even with a new user, and even if I use zsh rather than bash! (Apple Terminal has very similar code for session history management under zsh.) So it seems that some other recent change to my system is causing this. But I suspect it's not simply a Sonoma problem, since it does not appear to be widespread. Yikes!


It also only seems to happen when I exit the shell by quitting Terminal.app, but not when I just use the `exit` command.


Note that it’s happening on two separate macOS Sonoma machines, although the setups are essentially identical on them.


Is anyone else seeing this? Or any ideas what's happening (or even debugging suggestions)?


MacBook Pro 14″, 14.1

Posted on Nov 3, 2023 12:41 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 8, 2024 3:24 AM

Some more important data:


I have been able to reproduce the problem on a completely new installation of macOS Sonoma (in a virtual machine, but I don’t think this matters), with three tabs spread over two windows. No other software installed, no dot-files in the user directory.


So this is clearly a macOS (or at least Terminal.app) issue. As far as I can tell, this means that if you’re not seeing it, it can only be because (a) you’re just not noticing it [or you don’t use Terminal.app!]; (b) you don’t have macOS set to reopen windows [i.e., you need to have System Settings > Close windows when quitting… unset]; (c) you don’t open more than one window/tab, or you close them all by exiting the shell explicitly; (d) you are using some of the specific dotfile or environment variable manipulations you can set to get rid of the save/restore behaviour [listed in the bashrc_Apple_Terminal file].


1 reply
Question marked as Top-ranking reply

Jan 8, 2024 3:24 AM in response to a.jaffe

Some more important data:


I have been able to reproduce the problem on a completely new installation of macOS Sonoma (in a virtual machine, but I don’t think this matters), with three tabs spread over two windows. No other software installed, no dot-files in the user directory.


So this is clearly a macOS (or at least Terminal.app) issue. As far as I can tell, this means that if you’re not seeing it, it can only be because (a) you’re just not noticing it [or you don’t use Terminal.app!]; (b) you don’t have macOS set to reopen windows [i.e., you need to have System Settings > Close windows when quitting… unset]; (c) you don’t open more than one window/tab, or you close them all by exiting the shell explicitly; (d) you are using some of the specific dotfile or environment variable manipulations you can set to get rid of the save/restore behaviour [listed in the bashrc_Apple_Terminal file].


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Apple Terminal startup errors (session save/restore)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.