0

How do I obtain a variable with the current command and arguments? I would like to use escape code to print this in an Xterm title bar.

I see Terminal.app on MacOS is determining the name of the invoked command somehow. I see the follow implementation for zsh, however I'm looking for ksh.

if [ "$SHELL" = '/usr/bin/zsh' ] then case $TERM in rxvt|*term) precmd() { print -Pn "\e]0;%m:%~\a" } preexec () { print -Pn "\e]0;$1\a" } ;; esac fi 

I'm reading this post on using ksh's DEBUG trap or poster also comments that "running a background task that regularly inspects the output of ps -o stat= -o args= to see which processes run in foreground and what command they are executing" is preferred. Does anyone have more information on this?

1 Answer 1

1

With ksh93:

set -o functrace trap 'printf "\e]0;%s\a" "${.sh.command}" > /dev/tty' DEBUG HOSTNAME=$(uname -n) PS1=$'\e]0;$HOSTNAME:${PWD/#"$HOME"/\\~}\a\e[32m$HOSTNAME\e[m:\e[35m${PWD/#"$HOME"/\\~}\e[m$ ' 

Would do something similar to your zsh code: displays simple commands that are being executed in the title just before they're executed (for pipe lines, it only prints the simple commands in the right-most component), and when returning to the prompt hostname:current-directory (with $HOME replaced with ~).

Relevant sections of the the man page:

.sh.command
When processing a DEBUG trap, this variable contains the current command line that is about to run. The value is in the same format as the output generated by the xtrace option (minus the preceding PS4 prompt).

functrace
Causes the -x option's state and the DEBUG trap action to be inherited by functions defined using the function keyword (see Functions above) instead of being reset to default. Changes made to them within the function do not propagate back to the parent scope. Similarly, this option also causes the DEBUG trap action to be inherited by subshells.

${.sh.command} being ksh93's equivalent to zsh's $ZSH_DEBUG_CMD.

The functrace option was added in 2022, so only available in 93u+m/1.0.0 2022-08-01, latest as of writing being 93u+m/1.0.10 2024-08-01.

ksh93u+m is the community effort that maintains ksh93 based on the code from ksh93u+ 2012-08-01, the last stable release by AT&T Software Technology, led by Martijn Dekker (the m in ksh93u+m). A separate effort to maintain ksh93 based on the last beta release from AST and which led to the short-lived ksh2020 has been abandoned.

Note DEBUG trap (also supported by zsh) and zsh's preexec work at different stages of shell execution.

The preexec hook is invoked when a user presses Enter to submit shell code at the prompt of an interactive shell for evaluation (whether that shell code contains one simple command or several or compound commands such as loop), while the DEBUG trap is invoked for each "command" (not all types of command) being run.

11
  • Curious, how would ksh88 accomplish this? Commented Jan 30 at 22:56
  • set: functrace: bad option(s). I'm using MacOSX, did you test this on Commented Jan 30 at 23:02
  • @atod, I don't know and it probably doesn't matter now (or ever for macos as I don't expect ksh88 was even ported there). No ${.sh.command} in ksh88 and IIRC the DEBUG trap is run after the command there, not before. Commented Jan 30 at 23:03
  • $ echo $KSH_VERSION Version AJM 93u+ 2012-08-01 Commented Jan 30 at 23:03
  • I don't use macos or any proprietary OS, the latest version of ksh93 where set -o functrace works can be found at github.com/ksh93/ksh Commented Jan 30 at 23:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.