Search code examples
bash

How to get the running terminal emulator while running a shell?


I am building an alternative system information reporter like neofetch, it works fine but there is one problem. It cannot get the current terminal emulator while running bash. I tried these commands:

readlink "/proc/$(cat /proc/$(echo $$)/stat|cut -d ' ' -f 4)/exe"
ps -o comm= -p $(ps -o ppid= -p $$)

They work perfectly fine when executed normally, but when I put them in a bash script, it just returns bash or /usr/bin/bash.

I want to find a command that always reports the terminal emulator, not the shell. Here is the full code if you want:

#!/bin/sh

# yes. no logos. because why not?

# source the config file
if [ "$colorsoff" = "" ]; then
    colorsoff=0
fi

[ -e /etc/betterfetchrc ] && . /etc/betterfetchrc 2> /dev/null
[ -e ~/.betterfetchrc ] && . ~/.betterfetchrc 2> /dev/null

CURRENT_VERSION_FILE="/etc/betterfetch-version"  # Path to the file containing the current version
REMOTE_VERSION_URL="https://raw.githubusercontent.com/sctech-tr/betterfetch/main/betterfetch-version"

# Fetch the remote version from the URL
REMOTE_VERSION=$(curl -s $REMOTE_VERSION_URL)

# Read the current version from the version file
if [ -f "$CURRENT_VERSION_FILE" ]; then
  CURRENT_VERSION=$(cat "$CURRENT_VERSION_FILE")
else
  echo "error: current version file not found."
  exit 1
fi

# Compare the remote version with the current version
if [ "$REMOTE_VERSION" != "$CURRENT_VERSION" ]; then
  echo "betterfetch ($REMOTE_VERSION) is available! you are currently on version $CURRENT_VERSION."
  exit 0
fi



# the meat and potatoes, actual fetch

# only set these if they are not already set by the config file
[ -z "$os" ] && . /etc/os-release 2>/dev/null || export os="Unknown"
[ -z "$host" ] && host=$(cat /proc/sys/kernel/hostname)
[ -z "$kernel" ] && kernel=$(sed "s/version // ; s/ (.*//" /proc/version)
[ -z "$uptime" ] && uptime=$(uptime -p 2>/dev/null | sed "s/up //")
[ -z "$shell" ] && shell=$(printf "$SHELL" | sed "s/\/bin\///" | sed "s/\/usr//")
[ -z "$de" ] && de=$(echo $XDG_CURRENT_DESKTOP)
[ -z "$terminal" ] && terminal=$(readlink "/proc/$(cat /proc/$(echo $$)/stat|cut -d ' ' -f 4)/exe")

printf "$USER@$host\n"
printf "OS           $os\n"
printf "Kernel       $kernel\n"
printf "Uptime       $uptime\n"
printf "Shell        $shell\n"
printf "DE           $de\n"
printf "Terminal     $terminal\n"
if [ "$colorsoff" != 1 ]; then
    printf "\033[0;31m● \033[0;32m● \033[0;33m● \033[0;34m● \033[0;35m● \033[0;36m● \033[0;37m●\033[0m\n"
fi


Solution

  • Generally, it is impossible to do. The only best efforted way you can traverse the pid hierarchy up until you find a terminal emulator from a known list.

    The following looks ok. Add more terminals to the list you want to handle.

    terminals="konsole xterm etc.."  # space separated list of terminal emulators
    terminal=unknown
    cur=$$
    while cur=$(ps -o ppid:1= -p "$cur") && ((cur)); do
      comm=$(<"/proc/$cur/comm")
      if [[ " $terminals " = *" $comm "* ]]; then
        terminal=$comm
        break
      fi
    done
    echo "$terminal"