├── .github └── workflows │ └── bats.yaml ├── test ├── include-test.bats ├── README.md └── bash-preexec.bats ├── LICENSE.md ├── README.md └── bash-preexec.sh /.github/workflows/bats.yaml: -------------------------------------------------------------------------------- 1 | name: Bats tests 2 | on: [push, pull_request] 3 | 4 | jobs: 5 | test: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - name: Install Bats 9 | uses: bats-core/bats-action@3.0.1 10 | - name: Check out repository 11 | uses: actions/checkout@v5 12 | - name: Run tests 13 | run: bats test 14 | -------------------------------------------------------------------------------- /test/include-test.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | @test "should not import if it's already defined" { 4 | bash_preexec_imported="defined" 5 | source "${BATS_TEST_DIRNAME}/../bash-preexec.sh" 6 | [ -z $(type -t __bp_install) ] 7 | } 8 | 9 | @test "should not import if it's already defined (old guard, don't use elsewhere!)" { 10 | __bp_imported="defined" 11 | source "${BATS_TEST_DIRNAME}/../bash-preexec.sh" 12 | [ -z $(type -t __bp_install) ] 13 | } 14 | 15 | @test "should import if not defined" { 16 | unset bash_preexec_imported 17 | source "${BATS_TEST_DIRNAME}/../bash-preexec.sh" 18 | [ -n $(type -t __bp_install) ] 19 | } 20 | 21 | @test "bp should stop installation if HISTTIMEFORMAT is readonly" { 22 | readonly HISTTIMEFORMAT 23 | run source "${BATS_TEST_DIRNAME}/../bash-preexec.sh" 24 | [ $status -ne 0 ] 25 | [[ "$output" =~ "HISTTIMEFORMAT" ]] || return 1 26 | } 27 | -------------------------------------------------------------------------------- /test/README.md: -------------------------------------------------------------------------------- 1 | Testing `bash-preexec` 2 | ====================== 3 | 4 | **Note on test conditions** 5 | 6 | When writing test conditions, use `[ ... ]` instead of `[[ ... ]]` since the 7 | former are supported by Bats on Bash versions before 4.1. In particular, macOS 8 | uses Bash 3.2, and `[[ ... ]]` tests always pass on macOS. 9 | 10 | In some cases, you may want to use a feature unique to `[[ ... ]]` such as 11 | pattern matching (`[[ $name = a* ]]`) or regular expressions (`[[ $(date) =~ 12 | ^Fri\ ...\ 13 ]]`). In those cases, use the following pattern to replace “bare” 13 | `[[ ... ]]`. 14 | 15 | ``` 16 | [[ ... ]] || return 1 17 | ``` 18 | 19 | References: 20 | * [Differences between `[` and `[[`](http://mywiki.wooledge.org/BashFAQ/031) 21 | * [Problems with `[[` in Bats](https://github.com/sstephenson/bats/issues/49) 22 | * [Using `|| return 1` instead of `|| false`](https://github.com/bats-core/bats-core/commit/e5695a673faad4d4d33446ed5c99d70dbfa6d8be) 23 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2017 Ryan Caloras and contributors (see https://github.com/rcaloras/bash-preexec) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://github.com/rcaloras/bash-preexec/actions/workflows/bats.yaml/badge.svg)](https://github.com/rcaloras/bash-preexec/actions/) 2 | [![GitHub version](https://badge.fury.io/gh/rcaloras%2Fbash-preexec.svg)](https://badge.fury.io/gh/rcaloras%2Fbash-preexec) 3 | 4 | Bash-Preexec 5 | ============ 6 | 7 | **preexec** and **precmd** hook functions for Bash 3.1+ in the style of Zsh. They aim to emulate the behavior [as described for Zsh](http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions). 8 | 9 | Bashhub Logo 10 | 11 | This project is currently being used in production by [Bashhub](https://github.com/rcaloras/bashhub-client), [iTerm2](https://github.com/gnachman/iTerm2), and [Ghostty](https://ghostty.org/). Hype! 12 | 13 | ## Quick Start 14 | ```bash 15 | # Pull down our file from GitHub and write it to your home directory as a hidden file. 16 | curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh 17 | # Source our file to bring it into our environment 18 | source ~/.bash-preexec.sh 19 | # Define a couple functions. 20 | preexec() { echo "just typed $1"; } 21 | precmd() { echo "printing the prompt"; } 22 | ``` 23 | 24 | ## Install 25 | You'll want to pull down the file and add it to your bash profile/configuration (i.e ~/.bashrc, ~/.profile, ~/.bash_profile, etc). **It must be the last thing imported in your bash profile.** 26 | ```bash 27 | # Pull down our file from GitHub and write it to your home directory as a hidden file. 28 | curl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh 29 | # Source our file at the end of our bash profile (e.g. ~/.bashrc, ~/.profile, or ~/.bash_profile) 30 | echo '[[ -f ~/.bash-preexec.sh ]] && source ~/.bash-preexec.sh' >> ~/.bashrc 31 | ``` 32 | 33 | ## Usage 34 | Two functions **preexec** and **precmd** can now be defined and they'll be automatically invoked by bash-preexec if they exist. 35 | 36 | * `preexec` Executed just after a command has been read and is about to be executed. The string that the user typed is passed as the first argument. 37 | * `precmd` Executed just before each prompt. Equivalent to PROMPT_COMMAND, but more flexible and resilient. 38 | ```bash 39 | source ~/.bash-preexec.sh 40 | preexec() { echo "just typed $1"; } 41 | precmd() { echo "printing the prompt"; } 42 | ``` 43 | Should output something like: 44 | ``` 45 | elementz@Kashmir:~/git/bash-preexec (master)$ ls 46 | just typed ls 47 | bash-preexec.sh README.md test 48 | printing the prompt 49 | ``` 50 | #### Function Arrays 51 | You can also define functions to be invoked by appending them to two different arrays. This is great if you want to have many functions invoked for either hook. Both preexec and precmd functions are added to these by default and don't need to be added manually. 52 | * `$preexec_functions` Array of functions invoked by preexec. 53 | * `$precmd_functions` Array of functions invoked by precmd. 54 | 55 | #### preexec 56 | ```bash 57 | # Define some function to use preexec 58 | preexec_hello_world() { echo "You just entered $1"; } 59 | # Add it to the array of functions to be invoked each time. 60 | preexec_functions+=(preexec_hello_world) 61 | ``` 62 | 63 | #### precmd 64 | ```bash 65 | precmd_hello_world() { echo "This is invoked before the prompt is displayed"; } 66 | precmd_functions+=(precmd_hello_world) 67 | ``` 68 | 69 | You can also define multiple functions to be invoked like so. 70 | 71 | ```bash 72 | precmd_hello_one() { echo "This is invoked on precmd first"; } 73 | precmd_hello_two() { echo "This is invoked on precmd second"; } 74 | precmd_functions+=(precmd_hello_one) 75 | precmd_functions+=(precmd_hello_two) 76 | ``` 77 | 78 | You can check the functions set for each by echoing its contents. 79 | 80 | ```bash 81 | echo ${preexec_functions[@]} 82 | echo ${precmd_functions[@]} 83 | ``` 84 | 85 | ## Subshells 86 | bash-preexec does not support invoking preexec() for subshells by default. It must be enabled by setting 87 | `__bp_enable_subshells`. 88 | ```bash 89 | # Enable experimental subshell support 90 | export __bp_enable_subshells="true" 91 | ``` 92 | This is disabled by default due to buggy situations related to to `functrace` and Bash's `DEBUG trap`. See [Issue #25](https://github.com/rcaloras/bash-preexec/issues/25) 93 | 94 | ## Library authors 95 | If you want to detect bash-preexec in your library (for example, to add hooks to `preexec_functions` when available), use the Bash variable `bash_preexec_imported`: 96 | 97 | ```bash 98 | if [[ -n "${bash_preexec_imported:-}" ]]; then 99 | echo "Bash-preexec is loaded." 100 | fi 101 | ``` 102 | 103 | ## Tests 104 | You can run tests using [Bats](https://github.com/bats-core/bats-core). 105 | ```bash 106 | bats test 107 | ``` 108 | Should output something like: 109 | ``` 110 | elementz@Kashmir:~/git/bash-preexec(master)$ bats test 111 | ✓ No functions defined for preexec should simply return 112 | ✓ precmd should execute a function once 113 | ✓ preexec should execute a function with the last command in our history 114 | ✓ preexec should execute multiple functions in the order added to their arrays 115 | ✓ preecmd should execute multiple functions in the order added to their arrays 116 | ``` 117 | -------------------------------------------------------------------------------- /test/bash-preexec.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | setup() { 4 | PROMPT_COMMAND='' # in case the invoking shell has set this 5 | history -s fake command # preexec requires there be some history 6 | set -o nounset # in case the user has this set 7 | __bp_delay_install="true" 8 | source "${BATS_TEST_DIRNAME}/../bash-preexec.sh" 9 | } 10 | 11 | # Evaluates all the elements of PROMPT_COMMAND 12 | eval_PROMPT_COMMAND() { 13 | local prompt_command 14 | for prompt_command in "${PROMPT_COMMAND[@]}"; do 15 | eval "$prompt_command" 16 | done 17 | } 18 | 19 | # Joins the elements of PROMPT_COMMAND with $'\n' 20 | join_PROMPT_COMMAND() { 21 | local IFS=$'\n' 22 | echo "${PROMPT_COMMAND[*]}" 23 | } 24 | 25 | bp_install() { 26 | __bp_install_after_session_init 27 | eval_PROMPT_COMMAND 28 | } 29 | 30 | test_echo() { 31 | echo "test echo" 32 | } 33 | 34 | test_preexec_echo() { 35 | printf "%s\n" "$1" 36 | } 37 | 38 | # Helper functions necessary because Bats' run doesn't preserve $? 39 | return_exit_code() { 40 | return $1 41 | } 42 | 43 | set_exit_code_and_run_precmd() { 44 | return_exit_code ${1:-0} 45 | __bp_precmd_invoke_cmd 46 | } 47 | 48 | 49 | @test "sourcing bash-preexec should exit with 1 if we're not using bash" { 50 | unset BASH_VERSION 51 | run source "${BATS_TEST_DIRNAME}/../bash-preexec.sh" 52 | [ $status -eq 1 ] 53 | [ -z "$output" ] 54 | } 55 | 56 | @test "sourcing bash-preexec should exit with 1 if we're using an older version of bash" { 57 | if type -p bash-3.0 &>/dev/null; then 58 | run bash-3.0 -c "source \"${BATS_TEST_DIRNAME}/../bash-preexec.sh\"" 59 | [ "$status" -eq 1 ] 60 | [ -z "$output" ] 61 | else 62 | skip 63 | fi 64 | } 65 | 66 | @test "__bp_install should exit if it's already installed" { 67 | bp_install 68 | 69 | run '__bp_install' 70 | [ $status -eq 1 ] 71 | [ -z "$output" ] 72 | } 73 | 74 | @test "__bp_install should remove trap logic and itself from PROMPT_COMMAND" { 75 | __bp_install_after_session_init 76 | 77 | # Assert that before running, the command contains the install string, and 78 | # afterwards it does not 79 | [[ "$PROMPT_COMMAND" == *"$__bp_install_string"* ]] || return 1 80 | 81 | eval_PROMPT_COMMAND 82 | 83 | [[ "$PROMPT_COMMAND" != *"$__bp_install_string"* ]] || return 1 84 | } 85 | 86 | @test "__bp_install should preserve an existing DEBUG trap" { 87 | trap_invoked_count=0 88 | foo() { (( trap_invoked_count += 1 )); } 89 | 90 | # note setting this causes BATS to mis-report the failure line when this test fails 91 | trap foo DEBUG 92 | [ "$(trap -p DEBUG | cut -d' ' -f3)" == "'foo'" ] 93 | 94 | bp_install 95 | trap_count_snapshot=$trap_invoked_count 96 | 97 | [ "$(trap -p DEBUG | cut -d' ' -f3)" == "'__bp_preexec_invoke_exec" ] 98 | [[ "${preexec_functions[*]}" == *"__bp_original_debug_trap"* ]] || return 1 99 | 100 | __bp_interactive_mode # triggers the DEBUG trap 101 | 102 | # ensure the trap count is still being incremented after the trap's been overwritten 103 | (( trap_count_snapshot < trap_invoked_count )) 104 | } 105 | 106 | @test "__bp_install should preserve an existing DEBUG trap containing quotes" { 107 | trap_invoked_count=0 108 | foo() { (( trap_invoked_count += 1 )); } 109 | 110 | # note setting this causes BATS to mis-report the failure line when this test fails 111 | trap "foo && echo 'hello' >/dev/null" debug 112 | [ "$(trap -p DEBUG | cut -d' ' -f3-7)" == "'foo && echo '\''hello'\'' >/dev/null'" ] 113 | 114 | bp_install 115 | trap_count_snapshot=$trap_invoked_count 116 | 117 | [ "$(trap -p DEBUG | cut -d' ' -f3)" == "'__bp_preexec_invoke_exec" ] 118 | [[ "${preexec_functions[*]}" == *"__bp_original_debug_trap"* ]] || return 1 119 | 120 | __bp_interactive_mode # triggers the DEBUG trap 121 | 122 | # ensure the trap count is still being incremented after the trap's been overwritten 123 | (( trap_count_snapshot < trap_invoked_count )) 124 | } 125 | 126 | @test "__bp_sanitize_string should remove semicolons and trim space" { 127 | 128 | __bp_sanitize_string output " true1; "$'\n' 129 | [ "$output" == "true1" ] 130 | 131 | __bp_sanitize_string output " ; true2; " 132 | [ "$output" == "true2" ] 133 | 134 | __bp_sanitize_string output $'\n'" ; true3; " 135 | [ "$output" == "true3" ] 136 | 137 | } 138 | 139 | @test "Appending to PROMPT_COMMAND should work after bp_install" { 140 | bp_install 141 | 142 | PROMPT_COMMAND="$PROMPT_COMMAND; true" 143 | eval_PROMPT_COMMAND 144 | } 145 | 146 | @test "Appending or prepending to PROMPT_COMMAND should work after bp_install_after_session_init" { 147 | __bp_install_after_session_init 148 | nl=$'\n' 149 | PROMPT_COMMAND="$PROMPT_COMMAND; true" 150 | PROMPT_COMMAND="$PROMPT_COMMAND $nl true" 151 | PROMPT_COMMAND="$PROMPT_COMMAND; true" 152 | PROMPT_COMMAND="true; $PROMPT_COMMAND" 153 | PROMPT_COMMAND="true; $PROMPT_COMMAND" 154 | PROMPT_COMMAND="true; $PROMPT_COMMAND" 155 | PROMPT_COMMAND="true $nl $PROMPT_COMMAND" 156 | eval_PROMPT_COMMAND 157 | } 158 | 159 | # Case where a user is appending or prepending to PROMPT_COMMAND. 160 | # This can happen after 'source bash-preexec.sh' e.g. 161 | # source bash-preexec.sh; PROMPT_COMMAND="$PROMPT_COMMAND; other_prompt_command_hook" 162 | @test "Adding to PROMPT_COMMAND before and after initiating install" { 163 | PROMPT_COMMAND="echo before" 164 | PROMPT_COMMAND="$PROMPT_COMMAND; echo before2" 165 | __bp_install_after_session_init 166 | PROMPT_COMMAND="$PROMPT_COMMAND"$'\n echo after' 167 | PROMPT_COMMAND="echo after2; $PROMPT_COMMAND;" 168 | 169 | eval_PROMPT_COMMAND 170 | 171 | expected_result=$'__bp_precmd_invoke_cmd\necho after2; echo before; echo before2\n echo after\n__bp_interactive_mode' 172 | [ "$(join_PROMPT_COMMAND)" == "$expected_result" ] 173 | } 174 | 175 | @test "Adding to PROMPT_COMMAND after with semicolon" { 176 | PROMPT_COMMAND="echo before" 177 | __bp_install_after_session_init 178 | PROMPT_COMMAND="$PROMPT_COMMAND; echo after" 179 | 180 | eval_PROMPT_COMMAND 181 | 182 | expected_result=$'__bp_precmd_invoke_cmd\necho before\n echo after\n__bp_interactive_mode' 183 | [ "$(join_PROMPT_COMMAND)" == "$expected_result" ] 184 | } 185 | 186 | @test "during install PROMPT_COMMAND and precmd functions should be executed each once" { 187 | PROMPT_COMMAND="echo before" 188 | PROMPT_COMMAND="$PROMPT_COMMAND; echo before2" 189 | __bp_install_after_session_init 190 | PROMPT_COMMAND="$PROMPT_COMMAND; echo after" 191 | PROMPT_COMMAND="echo after2; $PROMPT_COMMAND;" 192 | 193 | precmd() { echo "inside precmd"; } 194 | run eval_PROMPT_COMMAND 195 | [ "${lines[0]}" == "after2" ] 196 | [ "${lines[1]}" == "before" ] 197 | [ "${lines[2]}" == "before2" ] 198 | [ "${lines[3]}" == "inside precmd" ] 199 | [ "${lines[4]}" == "after" ] 200 | [ "${#lines[@]}" == '5' ] 201 | } 202 | 203 | @test "No functions defined for preexec should simply return" { 204 | __bp_interactive_mode 205 | 206 | run '__bp_preexec_invoke_exec' 'true' 207 | [ $status -eq 0 ] 208 | [ -z "$output" ] 209 | } 210 | 211 | @test "precmd should execute a function once" { 212 | precmd_functions+=(test_echo) 213 | run set_exit_code_and_run_precmd 214 | [ $status -eq 0 ] 215 | [ "$output" == "test echo" ] 216 | } 217 | 218 | @test "precmd should set \$? to be the previous exit code" { 219 | echo_exit_code() { 220 | echo "$?" 221 | } 222 | 223 | precmd_functions+=(echo_exit_code) 224 | run set_exit_code_and_run_precmd 251 225 | [ $status -eq 251 ] 226 | [ "$output" == "251" ] 227 | } 228 | 229 | @test "precmd should set \$BP_PIPESTATUS to the previous \$PIPESTATUS" { 230 | echo_pipestatus() { 231 | echo "${BP_PIPESTATUS[*]}" 232 | } 233 | # Helper function is necessary because Bats' run doesn't preserve $PIPESTATUS 234 | set_pipestatus_and_run_precmd() { 235 | false | true 236 | __bp_precmd_invoke_cmd 237 | } 238 | 239 | precmd_functions+=(echo_pipestatus) 240 | run 'set_pipestatus_and_run_precmd' 241 | [ $status -eq 0 ] 242 | [ "$output" == "1 0" ] 243 | } 244 | 245 | @test "precmd should set \$_ to be the previous last arg" { 246 | echo_last_arg() { 247 | echo "$_" 248 | } 249 | precmd_functions+=(echo_last_arg) 250 | 251 | bats_trap=$(trap -p DEBUG) 252 | trap DEBUG # remove the Bats stack-trace trap so $_ doesn't get overwritten 253 | : "last-arg" 254 | __bp_preexec_invoke_exec "$_" 255 | eval "$bats_trap" # Restore trap 256 | run set_exit_code_and_run_precmd 257 | [ $status -eq 0 ] 258 | [ "$output" == "last-arg" ] 259 | } 260 | 261 | @test "preexec should execute a function with the last command in our history" { 262 | preexec_functions+=(test_preexec_echo) 263 | __bp_interactive_mode 264 | git_command="git commit -a -m 'committing some stuff'" 265 | history -s $git_command 266 | 267 | run '__bp_preexec_invoke_exec' 268 | [ $status -eq 0 ] 269 | [ "$output" == "$git_command" ] 270 | } 271 | 272 | @test "preexec should execute multiple functions in the order added to their arrays" { 273 | fun_1() { echo "$1 one"; } 274 | fun_2() { echo "$1 two"; } 275 | preexec_functions+=(fun_1) 276 | preexec_functions+=(fun_2) 277 | __bp_interactive_mode 278 | 279 | run '__bp_preexec_invoke_exec' 280 | [ $status -eq 0 ] 281 | [ "${#lines[@]}" == '2' ] 282 | [ "${lines[0]}" == "fake command one" ] 283 | [ "${lines[1]}" == "fake command two" ] 284 | } 285 | 286 | @test "preecmd should execute multiple functions in the order added to their arrays" { 287 | fun_1() { echo "one"; } 288 | fun_2() { echo "two"; } 289 | precmd_functions+=(fun_1) 290 | precmd_functions+=(fun_2) 291 | 292 | run set_exit_code_and_run_precmd 293 | [ $status -eq 0 ] 294 | [ "${#lines[@]}" == '2' ] 295 | [ "${lines[0]}" == "one" ] 296 | [ "${lines[1]}" == "two" ] 297 | } 298 | 299 | @test "preexec should execute a function with IFS defined to local scope" { 300 | IFS=_ 301 | name_with_underscores_1() { parts=(1_2); echo $parts; } 302 | preexec_functions+=(name_with_underscores_1) 303 | 304 | __bp_interactive_mode 305 | run '__bp_preexec_invoke_exec' 306 | [ $status -eq 0 ] 307 | [ "$output" == "1 2" ] 308 | } 309 | 310 | @test "precmd should execute a function with IFS defined to local scope" { 311 | IFS=_ 312 | name_with_underscores_2() { parts=(2_2); echo $parts; } 313 | precmd_functions+=(name_with_underscores_2) 314 | run set_exit_code_and_run_precmd 315 | [ $status -eq 0 ] 316 | [ "$output" == "2 2" ] 317 | } 318 | 319 | @test "preexec should set \$? to be the exit code of preexec_functions" { 320 | return_nonzero() { 321 | return 1 322 | } 323 | preexec_functions+=(return_nonzero) 324 | 325 | __bp_interactive_mode 326 | 327 | run '__bp_preexec_invoke_exec' 328 | [ $status -eq 1 ] 329 | } 330 | 331 | @test "in_prompt_command should detect if a command is part of PROMPT_COMMAND" { 332 | 333 | PROMPT_COMMAND=$'precmd_invoke_cmd\n something; echo yo\n __bp_interactive_mode' 334 | run '__bp_in_prompt_command' "something" 335 | [ $status -eq 0 ] 336 | 337 | run '__bp_in_prompt_command' "something_else" 338 | [ $status -eq 1 ] 339 | 340 | # Should trim commands and arguments here. 341 | PROMPT_COMMAND=" precmd_invoke_cmd ; something ; some_stuff_here;" 342 | run '__bp_in_prompt_command' " precmd_invoke_cmd " 343 | [ $status -eq 0 ] 344 | 345 | PROMPT_COMMAND=" precmd_invoke_cmd ; something ; some_stuff_here;" 346 | run '__bp_in_prompt_command' " not_found" 347 | [ $status -eq 1 ] 348 | 349 | } 350 | 351 | @test "__bp_adjust_histcontrol should remove ignorespace and ignoreboth" { 352 | 353 | # Should remove ignorespace 354 | HISTCONTROL="ignorespace:ignoredups:*" 355 | __bp_adjust_histcontrol 356 | [ "$HISTCONTROL" == ":ignoredups:*" ] 357 | 358 | # Should remove ignoreboth and replace it with ignoredups 359 | HISTCONTROL="ignoreboth" 360 | __bp_adjust_histcontrol 361 | [ "$HISTCONTROL" == "ignoredups:" ] 362 | 363 | # Handle a few inputs 364 | HISTCONTROL="ignoreboth:ignorespace:some_thing_else" 365 | __bp_adjust_histcontrol 366 | echo "$HISTCONTROL" 367 | [ "$HISTCONTROL" == "ignoredups:::some_thing_else" ] 368 | 369 | } 370 | 371 | @test "preexec should respect HISTTIMEFORMAT" { 372 | preexec_functions+=(test_preexec_echo) 373 | __bp_interactive_mode 374 | git_command="git commit -a -m 'committing some stuff'" 375 | HISTTIMEFORMAT='%F %T ' 376 | history -s $git_command 377 | 378 | run '__bp_preexec_invoke_exec' 379 | [ $status -eq 0 ] 380 | [ "$output" == "$git_command" ] 381 | } 382 | 383 | @test "preexec should not strip whitespace from commands" { 384 | preexec_functions+=(test_preexec_echo) 385 | __bp_interactive_mode 386 | history -s " this command has whitespace " 387 | 388 | run '__bp_preexec_invoke_exec' 389 | [ $status -eq 0 ] 390 | [ "$output" == " this command has whitespace " ] 391 | } 392 | 393 | @test "preexec should preserve multi-line strings in commands" { 394 | preexec_functions+=(test_preexec_echo) 395 | __bp_interactive_mode 396 | history -s "this 'command contains 397 | a multiline string'" 398 | run '__bp_preexec_invoke_exec' 399 | [ $status -eq 0 ] 400 | [ "$output" == "this 'command contains 401 | a multiline string'" ] 402 | } 403 | 404 | @test "preexec should work on options to 'echo' commands" { 405 | preexec_functions+=(test_preexec_echo) 406 | __bp_interactive_mode 407 | history -s -- '-n' 408 | run '__bp_preexec_invoke_exec' 409 | [ $status -eq 0 ] 410 | [ "$output" == '-n' ] 411 | } 412 | -------------------------------------------------------------------------------- /bash-preexec.sh: -------------------------------------------------------------------------------- 1 | # bash-preexec.sh -- Bash support for ZSH-like 'preexec' and 'precmd' functions. 2 | # https://github.com/rcaloras/bash-preexec 3 | # 4 | # 5 | # 'preexec' functions are executed before each interactive command is 6 | # executed, with the interactive command as its argument. The 'precmd' 7 | # function is executed before each prompt is displayed. 8 | # 9 | # Author: Ryan Caloras (ryan@bashhub.com) 10 | # Forked from Original Author: Glyph Lefkowitz 11 | # 12 | # V0.6.0 13 | # 14 | 15 | # General Usage: 16 | # 17 | # 1. Source this file at the end of your bash profile so as not to interfere 18 | # with anything else that's using PROMPT_COMMAND. 19 | # 20 | # 2. Add any precmd or preexec functions by appending them to their arrays: 21 | # e.g. 22 | # precmd_functions+=(my_precmd_function) 23 | # precmd_functions+=(some_other_precmd_function) 24 | # 25 | # preexec_functions+=(my_preexec_function) 26 | # 27 | # 3. Consider changing anything using the DEBUG trap or PROMPT_COMMAND 28 | # to use preexec and precmd instead. Preexisting usages will be 29 | # preserved, but doing so manually may be less surprising. 30 | # 31 | # Note: This module requires two Bash features which you must not otherwise be 32 | # using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. If you override 33 | # either of these after bash-preexec has been installed it will most likely break. 34 | 35 | # Tell shellcheck what kind of file this is. 36 | # shellcheck shell=bash 37 | 38 | # Make sure this is bash that's running and return otherwise. 39 | # Use POSIX syntax for this line: 40 | if [ -z "${BASH_VERSION-}" ]; then 41 | return 1 42 | fi 43 | 44 | # We only support Bash 3.1+. 45 | # Note: BASH_VERSINFO is first available in Bash-2.0. 46 | if [[ -z "${BASH_VERSINFO-}" ]] || (( BASH_VERSINFO[0] < 3 || (BASH_VERSINFO[0] == 3 && BASH_VERSINFO[1] < 1) )); then 47 | return 1 48 | fi 49 | 50 | # Avoid duplicate inclusion 51 | if [[ -n "${bash_preexec_imported:-}" || -n "${__bp_imported:-}" ]]; then 52 | return 0 53 | fi 54 | bash_preexec_imported="defined" 55 | 56 | # WARNING: This variable is no longer used and should not be relied upon. 57 | # Use ${bash_preexec_imported} instead. 58 | # shellcheck disable=SC2034 59 | __bp_imported="${bash_preexec_imported}" 60 | 61 | # Should be available to each precmd and preexec 62 | # functions, should they want it. $? and $_ are available as $? and $_, but 63 | # $PIPESTATUS is available only in a copy, $BP_PIPESTATUS. 64 | # TODO: Figure out how to restore PIPESTATUS before each precmd or preexec 65 | # function. 66 | __bp_last_ret_value="$?" 67 | BP_PIPESTATUS=("${PIPESTATUS[@]}") 68 | __bp_last_argument_prev_command="$_" 69 | 70 | __bp_inside_precmd=0 71 | __bp_inside_preexec=0 72 | 73 | # Initial PROMPT_COMMAND string that is removed from PROMPT_COMMAND post __bp_install 74 | __bp_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install' 75 | 76 | # Fails if any of the given variables are readonly 77 | # Reference https://stackoverflow.com/a/4441178 78 | __bp_require_not_readonly() { 79 | local var 80 | for var; do 81 | if ! ( unset "$var" 2> /dev/null ); then 82 | echo "bash-preexec requires write access to ${var}" >&2 83 | return 1 84 | fi 85 | done 86 | } 87 | 88 | # Remove ignorespace and or replace ignoreboth from HISTCONTROL 89 | # so we can accurately invoke preexec with a command from our 90 | # history even if it starts with a space. 91 | __bp_adjust_histcontrol() { 92 | local histcontrol 93 | histcontrol="${HISTCONTROL:-}" 94 | histcontrol="${histcontrol//ignorespace}" 95 | # Replace ignoreboth with ignoredups 96 | if [[ "$histcontrol" == *"ignoreboth"* ]]; then 97 | histcontrol="ignoredups:${histcontrol//ignoreboth}" 98 | fi 99 | export HISTCONTROL="$histcontrol" 100 | } 101 | 102 | # This variable describes whether we are currently in "interactive mode"; 103 | # i.e. whether this shell has just executed a prompt and is waiting for user 104 | # input. It documents whether the current command invoked by the trace hook is 105 | # run interactively by the user; it's set immediately after the prompt hook, 106 | # and unset as soon as the trace hook is run. 107 | __bp_preexec_interactive_mode="" 108 | 109 | # These arrays are used to add functions to be run before, or after, prompts. 110 | declare -a precmd_functions 111 | declare -a preexec_functions 112 | 113 | # Trims leading and trailing whitespace from $2 and writes it to the variable 114 | # name passed as $1 115 | __bp_trim_whitespace() { 116 | local var=${1:?} text=${2:-} 117 | text="${text#"${text%%[![:space:]]*}"}" # remove leading whitespace characters 118 | text="${text%"${text##*[![:space:]]}"}" # remove trailing whitespace characters 119 | printf -v "$var" '%s' "$text" 120 | } 121 | 122 | 123 | # Trims whitespace and removes any leading or trailing semicolons from $2 and 124 | # writes the resulting string to the variable name passed as $1. Used for 125 | # manipulating substrings in PROMPT_COMMAND 126 | __bp_sanitize_string() { 127 | local var=${1:?} text=${2:-} sanitized 128 | __bp_trim_whitespace sanitized "$text" 129 | sanitized=${sanitized%;} 130 | sanitized=${sanitized#;} 131 | __bp_trim_whitespace sanitized "$sanitized" 132 | printf -v "$var" '%s' "$sanitized" 133 | } 134 | 135 | # This function is installed as part of the PROMPT_COMMAND; 136 | # It sets a variable to indicate that the prompt was just displayed, 137 | # to allow the DEBUG trap to know that the next command is likely interactive. 138 | __bp_interactive_mode() { 139 | __bp_preexec_interactive_mode="on" 140 | } 141 | 142 | 143 | # This function is installed as part of the PROMPT_COMMAND. 144 | # It will invoke any functions defined in the precmd_functions array. 145 | __bp_precmd_invoke_cmd() { 146 | # Save the returned value from our last command, and from each process in 147 | # its pipeline. Note: this MUST be the first thing done in this function. 148 | # BP_PIPESTATUS may be unused, ignore 149 | # shellcheck disable=SC2034 150 | 151 | __bp_last_ret_value="$?" BP_PIPESTATUS=("${PIPESTATUS[@]}") 152 | 153 | # Don't invoke precmds if we are inside an execution of an "original 154 | # prompt command" by another precmd execution loop. This avoids infinite 155 | # recursion. 156 | if (( __bp_inside_precmd > 0 )); then 157 | return 158 | fi 159 | local __bp_inside_precmd=1 160 | 161 | # Invoke every function defined in our function array. 162 | local precmd_function 163 | for precmd_function in "${precmd_functions[@]}"; do 164 | 165 | # Only execute this function if it actually exists. 166 | # Test existence of functions with: declare -[Ff] 167 | if type -t "$precmd_function" 1>/dev/null; then 168 | __bp_set_ret_value "$__bp_last_ret_value" "$__bp_last_argument_prev_command" 169 | # Quote our function invocation to prevent issues with IFS 170 | "$precmd_function" 171 | fi 172 | done 173 | 174 | __bp_set_ret_value "$__bp_last_ret_value" 175 | } 176 | 177 | # Sets a return value in $?. We may want to get access to the $? variable in our 178 | # precmd functions. This is available for instance in zsh. We can simulate it in bash 179 | # by setting the value here. 180 | __bp_set_ret_value() { 181 | return ${1:+"$1"} 182 | } 183 | 184 | __bp_in_prompt_command() { 185 | 186 | local prompt_command_array IFS=$'\n;' 187 | read -rd '' -a prompt_command_array <<< "${PROMPT_COMMAND[*]:-}" 188 | 189 | local trimmed_arg 190 | __bp_trim_whitespace trimmed_arg "${1:-}" 191 | 192 | local command trimmed_command 193 | for command in "${prompt_command_array[@]:-}"; do 194 | __bp_trim_whitespace trimmed_command "$command" 195 | if [[ "$trimmed_command" == "$trimmed_arg" ]]; then 196 | return 0 197 | fi 198 | done 199 | 200 | return 1 201 | } 202 | 203 | # This function is installed as the DEBUG trap. It is invoked before each 204 | # interactive prompt display. Its purpose is to inspect the current 205 | # environment to attempt to detect if the current command is being invoked 206 | # interactively, and invoke 'preexec' if so. 207 | __bp_preexec_invoke_exec() { 208 | 209 | # Save the contents of $_ so that it can be restored later on. 210 | # https://stackoverflow.com/questions/40944532/bash-preserve-in-a-debug-trap#40944702 211 | __bp_last_argument_prev_command="${1:-}" 212 | # Don't invoke preexecs if we are inside of another preexec. 213 | if (( __bp_inside_preexec > 0 )); then 214 | return 215 | fi 216 | local __bp_inside_preexec=1 217 | 218 | # Checks if the file descriptor is not standard out (i.e. '1') 219 | # __bp_delay_install checks if we're in test. Needed for bats to run. 220 | # Prevents preexec from being invoked for functions in PS1 221 | if [[ ! -t 1 && -z "${__bp_delay_install:-}" ]]; then 222 | return 223 | fi 224 | 225 | if [[ -n "${COMP_POINT:-}" || -n "${READLINE_POINT:-}" ]]; then 226 | # We're in the middle of a completer or a keybinding set up by "bind 227 | # -x". This obviously can't be an interactively issued command. 228 | return 229 | fi 230 | if [[ -z "${__bp_preexec_interactive_mode:-}" ]]; then 231 | # We're doing something related to displaying the prompt. Let the 232 | # prompt set the title instead of me. 233 | return 234 | else 235 | # If we're in a subshell, then the prompt won't be re-displayed to put 236 | # us back into interactive mode, so let's not set the variable back. 237 | # In other words, if you have a subshell like 238 | # (sleep 1; sleep 2) 239 | # You want to see the 'sleep 2' as a set_command_title as well. 240 | if [[ 0 -eq "${BASH_SUBSHELL:-}" ]]; then 241 | __bp_preexec_interactive_mode="" 242 | fi 243 | fi 244 | 245 | if __bp_in_prompt_command "${BASH_COMMAND:-}"; then 246 | # If we're executing something inside our prompt_command then we don't 247 | # want to call preexec. Bash prior to 3.1 can't detect this at all :/ 248 | __bp_preexec_interactive_mode="" 249 | return 250 | fi 251 | 252 | local this_command 253 | this_command=$(LC_ALL=C HISTTIMEFORMAT='' builtin history 1) 254 | this_command="${this_command#*[[:digit:]][* ] }" 255 | 256 | # Sanity check to make sure we have something to invoke our function with. 257 | if [[ -z "$this_command" ]]; then 258 | return 259 | fi 260 | 261 | # Invoke every function defined in our function array. 262 | local preexec_function 263 | local preexec_function_ret_value 264 | local preexec_ret_value=0 265 | for preexec_function in "${preexec_functions[@]:-}"; do 266 | 267 | # Only execute each function if it actually exists. 268 | # Test existence of function with: declare -[fF] 269 | if type -t "$preexec_function" 1>/dev/null; then 270 | __bp_set_ret_value "${__bp_last_ret_value:-}" 271 | # Quote our function invocation to prevent issues with IFS 272 | "$preexec_function" "$this_command" 273 | preexec_function_ret_value="$?" 274 | if [[ "$preexec_function_ret_value" != 0 ]]; then 275 | preexec_ret_value="$preexec_function_ret_value" 276 | fi 277 | fi 278 | done 279 | 280 | # Restore the last argument of the last executed command, and set the return 281 | # value of the DEBUG trap to be the return code of the last preexec function 282 | # to return an error. 283 | # If `extdebug` is enabled a non-zero return value from any preexec function 284 | # will cause the user's command not to execute. 285 | # Run `shopt -s extdebug` to enable 286 | __bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command" 287 | } 288 | 289 | __bp_install() { 290 | # Exit if we already have this installed. 291 | if [[ "${PROMPT_COMMAND[*]:-}" == *"__bp_precmd_invoke_cmd"* ]]; then 292 | return 1 293 | fi 294 | 295 | trap '__bp_preexec_invoke_exec "$_"' DEBUG 296 | 297 | # Preserve any prior DEBUG trap as a preexec function 298 | eval "local trap_argv=(${__bp_trap_string:-})" 299 | local prior_trap=${trap_argv[2]:-} 300 | unset __bp_trap_string 301 | if [[ -n "$prior_trap" ]]; then 302 | eval '__bp_original_debug_trap() { 303 | '"$prior_trap"' 304 | }' 305 | preexec_functions+=(__bp_original_debug_trap) 306 | fi 307 | 308 | # Adjust our HISTCONTROL Variable if needed. 309 | __bp_adjust_histcontrol 310 | 311 | # Issue #25. Setting debug trap for subshells causes sessions to exit for 312 | # backgrounded subshell commands (e.g. (pwd)& ). Believe this is a bug in Bash. 313 | # 314 | # Disabling this by default. It can be enabled by setting this variable. 315 | if [[ -n "${__bp_enable_subshells:-}" ]]; then 316 | 317 | # Set so debug trap will work be invoked in subshells. 318 | set -o functrace > /dev/null 2>&1 319 | shopt -s extdebug > /dev/null 2>&1 320 | fi 321 | 322 | local existing_prompt_command 323 | # Remove setting our trap install string and sanitize the existing prompt command string 324 | existing_prompt_command="${PROMPT_COMMAND:-}" 325 | # Edge case of appending to PROMPT_COMMAND 326 | existing_prompt_command="${existing_prompt_command//$__bp_install_string/:}" # no-op 327 | existing_prompt_command="${existing_prompt_command//$'\n':$'\n'/$'\n'}" # remove known-token only 328 | existing_prompt_command="${existing_prompt_command//$'\n':;/$'\n'}" # remove known-token only 329 | __bp_sanitize_string existing_prompt_command "$existing_prompt_command" 330 | if [[ "${existing_prompt_command:-:}" == ":" ]]; then 331 | existing_prompt_command= 332 | fi 333 | 334 | # Install our hooks in PROMPT_COMMAND to allow our trap to know when we've 335 | # actually entered something. 336 | PROMPT_COMMAND='__bp_precmd_invoke_cmd' 337 | PROMPT_COMMAND+=${existing_prompt_command:+$'\n'$existing_prompt_command} 338 | if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 1) )); then 339 | PROMPT_COMMAND+=('__bp_interactive_mode') 340 | else 341 | # shellcheck disable=SC2179 # PROMPT_COMMAND is not an array in bash <= 5.0 342 | PROMPT_COMMAND+=$'\n__bp_interactive_mode' 343 | fi 344 | 345 | # Add two functions to our arrays for convenience 346 | # of definition. 347 | precmd_functions+=(precmd) 348 | preexec_functions+=(preexec) 349 | 350 | # Invoke our two functions manually that were added to $PROMPT_COMMAND 351 | __bp_precmd_invoke_cmd 352 | __bp_interactive_mode 353 | } 354 | 355 | # Sets an installation string as part of our PROMPT_COMMAND to install 356 | # after our session has started. This allows bash-preexec to be included 357 | # at any point in our bash profile. 358 | __bp_install_after_session_init() { 359 | # bash-preexec needs to modify these variables in order to work correctly 360 | # if it can't, just stop the installation 361 | __bp_require_not_readonly PROMPT_COMMAND HISTCONTROL HISTTIMEFORMAT || return 362 | 363 | local sanitized_prompt_command 364 | __bp_sanitize_string sanitized_prompt_command "${PROMPT_COMMAND:-}" 365 | if [[ -n "$sanitized_prompt_command" ]]; then 366 | # shellcheck disable=SC2178 # PROMPT_COMMAND is not an array in bash <= 5.0 367 | PROMPT_COMMAND=${sanitized_prompt_command}$'\n' 368 | fi 369 | # shellcheck disable=SC2179 # PROMPT_COMMAND is not an array in bash <= 5.0 370 | PROMPT_COMMAND+=${__bp_install_string} 371 | } 372 | 373 | # Run our install so long as we're not delaying it. 374 | if [[ -z "${__bp_delay_install:-}" ]]; then 375 | __bp_install_after_session_init 376 | fi 377 | --------------------------------------------------------------------------------