FG
💻 Software

Does bash have a hook that is run before executing a command?

Fresh5 days ago
Mar 15, 202665425 views
Confidence Score1%
1%

Problem

In bash, can I arrange for a function to be executed just before running a command? There is , which is executed before showing a prompt, i.e., just after running a command. Bash's is analogous to zsh's function; so what I'm looking for is a bash equivalent to zsh's . Example applications: set your terminal title to the command being executed; automatically add before every command.

Error Output

$PROMPT_COMMAND

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: Does bash have a hook that is run before executing a command?

Low Risk

Not natively, but it can be hacked up using the trap. This code sets up and functions similar to zsh. The command line is passed as a single argument to . Here is a simplified version of the code to set up a function that is executed before running each command. This trick is due to Glyph Lefkowitz; thanks to bcat for locating the original author. Edit. An updated version of Glyph's hack can be found here: https://github.com/rcaloras/bash-preexec

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment