bash-preexec

repository·master·Indexed 21 days ago

https://github.com/rcaloras/bash-preexec

Provides Zsh-style preexec and precmd hook functions for Bash 3.1+. It allows developers to run code immediately before a command executes or just before a prompt is displayed via the preexec() and precmd() functions, or by appending function names to the preexec_functions and precmd_functions arrays.

Tokens
1K
Snippets
5
Records
5
Agent score
27%

What's inside bash-preexec

  1. How preexec and precmd hooks work

    master

    Bash-preexec provides two primary hook functions that emulate Zsh behavior:

    1. preexec: Executed immediately after a command has been read and just before it is executed. The command string typed by the user is passed as the first argument ($1).
    2. precmd: Executed just before each prompt is displayed. This is a more flexible and resilient alternative to the standard PROMPT_COMMAND.

    If these functions are defined in your environment, bash-preexec will automatically invoke them.

    source ~/.bash-preexec.sh
    
    preexec() { echo "just typed $1"; }
    precmd() { echo "printing the prompt"; }
  2. Install bash-preexec

    master

    To install bash-preexec, download the script to your home directory and source it in your bash profile (e.g., ~/.bashrc, ~/.profile, or ~/.bash_profile).

    Important: The sourcing command must be the last thing imported in your bash profile to ensure it functions correctly.

    # Pull down the file to your home directory as a hidden file
    curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh
    
    # Source it at the end of your bash profile (e.g., ~/.bashrc)
    echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc
  3. Enable experimental subshell support

    master

    By default, bash-preexec does not invoke preexec() for subshells. You can enable this experimental support by setting the __bp_enable_subshells environment variable to "true".

    Warning: This feature is disabled by default because it can cause issues with functrace and Bash's DEBUG trap.

    export __bp_enable_subshells="true"
  4. Use function arrays for multiple hooks

    master

    If you need to execute multiple distinct functions for a single hook, you can append function names to the following arrays. Note that the standard preexec() and precmd() functions are added to these arrays by default and do not need to be manually added.

    • $preexec_functions: An array of functions invoked by the preexec hook.
    • $precmd_functions: An array of functions invoked by the precmd hook.

    You can inspect the current contents of these arrays using ${preexec_functions[@]} or ${precmd_functions[@]}.

    # Using preexec arrays
    preexec_hello_world() { echo "You just entered $1"; }
    preexec_functions+=(preexec_hello_world)
    
    # Using precmd arrays
    precmd_hello_world() { echo "This is invoked before the prompt is displayed"; }
    precmd_functions+=(precmd_hello_world)
    
    # Adding multiple functions to precmd
    precmd_hello_one() { echo "This is invoked on precmd first"; }
    precmd_hello_two() { echo "This is invoked on precmd second"; }
    precmd_functions+=(precmd_hello_one)
    precmd_functions+=(precmd_hello_two)
  5. Detect bash-preexec in your library

    master

    If you are developing a library and want to detect if bash-preexec is loaded (to conditionally add hooks to preexec_functions or precmd_functions), check the bash_preexec_imported variable.

    if [[ -n "${bash_preexec_imported:-}" ]]; then
        echo "Bash-preexec is loaded."
    fi