Powerlevel10k: Indicate if the current session is logged

Created on 8 Sep 2020  路  9Comments  路  Source: romkatv/powerlevel10k

Sometimes I use the script command to log my session and an indicator that the session is being logged.

All 9 comments

Which command can one use to figure out whether the current session is being logged or not?

The only change that I could understand was that SHLVL env var was incremented but that increments every time you spawn a sub-shell. Maybe there will be changes in the stty?

One quick hack that I thought was to check the parent process (ps -p $(ps --no-headers -p $$ -o ppid)):
Inside script:

    PID TTY          TIME CMD
 748134 pts/7    00:00:00 script

Regular terminal (I use kitty):

    PID TTY          TIME CMD
 745210 ?        00:00:03 kitty

The only change that I could understand was that SHLVL env var was incremented but that increments every time you spawn a sub-shell.

This variable also updates when you spawn a new shell with plain zsh command, so it cannot be used to detect script.

Maybe there will be changes in the stty?

As far as I can tell, no.

One quick hack that I thought was to check the parent process (ps -p $(ps --no-headers -p $$ -o ppid)):

Doesn't work if spawn another zsh from a shell started by script. Also slow.

Going to close as infeasible. Feel free to reopen if you can provide instructions for detecting when shell is running under script.

From man script

     The following environment variables are utilized by script:

     SCRIPT
            The SCRIPT environment variable is added to the sub-shell.  If SCRIPT already existed in the users environment, its value is overwritten within the sub-shell.  The value of SCRIPT is the name of the typescript file.

     SHELL  If the variable SHELL exists, the shell forked by script will be that shell.  If SHELL is not set, the Bourne shell is assumed.  (Most shells set this variable automatically).

Interesting. My version of script doesn't set SCRIPT environment variable and doesn't list it in the man page. What I see is the same as this: https://man7.org/linux/man-pages/man1/script.1.html

% script --version
script from util-linux 2.34
% dpkg -S =script
bsdutils: /usr/bin/script
% script
Script started, file is typescript
% env | grep SCRIPT
% exit
Script done, file is typescript

script shipped with macOS is BSD variant that sets this env var https://www.freebsd.org/cgi/man.cgi?script(1)

script shipped with macOS is BSD variant that sets this env var https://www.freebsd.org/cgi/man.cgi?script(1)

Good to know.

@dzervas If you want to display an icon whenever SCRIPT environment variable is set, you can do it with a custom prompt segment. First you'll need to add this function to ~/.p10k.zsh:

function prompt_my_script() {
  if [[ -n $SCRIPT ]]; then
    p10k segment -b yellow -f red -i $'\uF6B7'
  fi
}

Then add my_script to POWERLEVEL9K_LEFT_PROMPT_ELEMENTS or POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS at the top of ~/.p10k.zsh.

Oh, my version of script (on Manjaro) does not export the env var either. Thank for your info anyway!

You can define your own environment variable to indicate that shell is running under script. To do this, you'll need to replace script [args]... with INSIDE_SCRIPT=1 script [args]... when starting script.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mystic8b picture Mystic8b  路  4Comments

rpathak38 picture rpathak38  路  4Comments

zemuldo picture zemuldo  路  4Comments

sanathkumarbs picture sanathkumarbs  路  3Comments

debo picture debo  路  5Comments