Bug 59613 - Customizable pre-execution hook
Customizable pre-execution hook
Product: Red Hat Linux
Classification: Retired
Component: bash (Show other bugs)
All Linux
medium Severity medium
: ---
: ---
Assigned To: Bernhard Rosenkraenzer
: FutureFeature
Depends On:
  Show dependency treegraph
Reported: 2002-02-11 04:35 EST by Roland Kaufmann
Modified: 2008-05-01 11:38 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Enhancement
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2002-07-07 08:24:53 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)
Patch against bash 2.05 to enable pre-execution hook (5.61 KB, patch)
2002-02-11 04:37 EST, Roland Kaufmann
no flags Details | Diff
Stripped-down patch with minimum required functionality (1.78 KB, patch)
2002-06-17 08:54 EDT, Roland Kaufmann
no flags Details | Diff
Reduced patch (only 3 lines!); now equivalent to PROMPT_COMMAND (491 bytes, patch)
2002-06-17 13:08 EDT, Roland Kaufmann
no flags Details | Diff
Patch to enable SPAWN_COMMAND environment variable (558 bytes, patch)
2002-07-07 08:22 EDT, Roland Kaufmann
no flags Details | Diff
Patch to enable LASTCMD environment variable (757 bytes, patch)
2002-07-07 08:24 EDT, Roland Kaufmann
no flags Details | Diff

  None (edit)
Description Roland Kaufmann 2002-02-11 04:35:58 EST
The environment variable PROMPT_COMMAND provides a way to run an
arbitrary command before the prompt is shown (post-execution hook),
but there is no way to run a command after the command-line has been
entered but before it has been executed (pre-execution hook).

I want to set the xterm title to show the command currently running,
so that I can see what a minimized or obscured window is doing by
looking at the taskbar.
Other uses for this could be menu systems, computer-based exercises
or monitoring.

Before each command is executed, the environment variable
SPAWN_COMMAND is inspected and executed if filled. The command-line
is accessible for this command through the use of the environment
variable CMD_LINE.

A new global variable 'global_command_line' is introduced, and the
history module sets this variable to be the contents of the last
command-line parsed.
A new function 'execute_spawn_command' is introduced, that reads the
contents of the SPAWN_COMMAND environment variable, sets the CMD_LINE
variable to the last command-line known, executes the commands and
restores the old value of CMD_LINE.
A call to execute_spawn_command() is added right before the call to
execute_command() in the main loop. This a place that was found to be
safe for such hooking. Adding it in execute_command() or further down
the chain gives odd effects due to recursive calls.
The option '--no-spawn-command' was added to enable the user to
disable the functionality.

The patch is done against bash version 2.05.

The use of a savestring() unmatched by an xfree() as an argument to
bind_variable() seems logical since bind_variable() would adopt the
responsibility of the memory for the variable.
However, the same thing done for parse_and_execute() does not seem
logical to me, but the string is appearently corrupted if this is not
done. Please confirm that this is safe to do in respect to memory
A new global variable to hold the contents of the command-line seems
redundant, but I'm having problems accessing the history records from
outside the history module, and I didn't bother to use any time to
figure out why it seemed state dependent.
A core dump may be triggered upon exit from a subshell if variable
substitution is used within the SPAWN_COMMAND variable.

Old scripts that happens to use the environment variable
SPAWN_COMMAND, will undoubtly behave very strange. For this reason,
the option --no-spawn-command is introduced to enable bash to revert
to 2.05 behavior, where SPAWN_COMMAND is not interpreted.

I suggest that the following paragraphs is added to the manual:

	If this option is NOT set (which is the default), the
	contents of the environment variable SPAWN_COMMAND is
	executed before the execution of the command-line. The
	environment variable CMD_LINE will be filled with the
	unexpanded contents of the command-line, for the duration of
	that command only (i.e. it reverts to its previous contents before
	the command-line executes).
	To display the current running process in the title bar of an
	xterm window, and reset the title before the prompt is shown
	again, put the following commands in your ~/.bashrc

	export SPAWN_COMMAND='echo -ne "\033]2;$CMD_LINE\007"'
	export PROMPT_COMMAND='echo -ne "\033]2;idle\007"'

	or, you may want to use a command that append information
	about the current user and directory for the command:

	export PROMPT_COMMAND='/bin/echo -ne "\033]0;idle in 
`/usr/bin/whoami`@`/bin/hostname | /bin/cut -d. -f 1`:`/bin/pwd | /bin/sed "s%
	export SPAWN_COMMAND='/bin/echo -ne "\033]2;${CMD_LINE} in 
`/usr/bin/whoami`@`/bin/hostname | /bin/cut -d. -f 1`:`/bin/pwd | /bin/sed "s%

Roland Kaufmann	<roland at ii dot uib dot no>
Hevard Lygre 	<hklygre at online dot no>
Comment 1 Roland Kaufmann 2002-02-11 04:37:08 EST
Created attachment 45199 [details]
Patch against bash 2.05 to enable pre-execution hook
Comment 2 Bernhard Rosenkraenzer 2002-02-12 06:01:24 EST
I've sent a message to the base package maintainers asking their opinion on 
I'm not sure about it myself, since there's both some good use (like what 
you're suggesting) and some potential for abuse and of course overhead. 
Waiting on feedback from base maintainers.
Comment 3 Roland Kaufmann 2002-02-17 08:33:14 EST
I don't think that it is more prone to abuse than
the PROMPT_COMMAND construct, except perhaps for
that commands can be cancelled (which was intended
as a feature).
When it comes to overhead, SPAWN_COMMAND is only
evaluated in interactive shells, and thus takes
typically a lot less time to evaluate than the
latency of the user.
I have used it myself for three months now, and
have never experienced it as interruptive of my
Comment 4 Roland Kaufmann 2002-06-17 08:54:31 EDT
Created attachment 61166 [details]
Stripped-down patch with minimum required functionality
Comment 5 Roland Kaufmann 2002-06-17 08:59:52 EDT
I have created a new patch (against the version of bash that comes with RedHat 
7.3), that only contains the around 10 lines of code that contains the minimum 
required functionality for the pre-execution hook to work. It gets rid of some 
ugly code to retrieve the last command-line, but instead relys on that history 
is enabled so that the user can set the variable like this: 
export SPAWN_COMMAND='echo -ne "\033]0;`history 1 | cut -c 8-` in 
Comment 6 Roland Kaufmann 2002-06-17 13:08:37 EDT
Created attachment 61245 [details]
Reduced patch (only 3 lines!); now equivalent to PROMPT_COMMAND
Comment 7 Roland Kaufmann 2002-07-07 08:22:38 EDT
Created attachment 64104 [details]
Patch to enable SPAWN_COMMAND environment variable
Comment 8 Roland Kaufmann 2002-07-07 08:24:49 EDT
Created attachment 64105 [details]
Patch to enable LASTCMD environment variable
Comment 9 Roland Kaufmann 2002-07-07 08:31:03 EDT
The latest two patches give back most of the functionality
of the original proposal.

SPAWN_COMMAND is an environment variable understood by the
shell in the same way as the PROMPT_COMMAND variable is already
except that it is executed before instead of after the command.

LASTCMD is an environment variable set by the shell in the same
way as the HISTCMD variable except that it will contain the
actual contents of the command line instead of its history number.

These patches has been tested against RedHat Linux 7.3.

Due to the appearant lack of interest in this functionality, further
patches will not be posted to Bugzilla but to my website only.

Note You need to log in before you can comment on or make changes to this bug.