{"id":13537268,"url":"https://github.com/rcaloras/bash-preexec","last_synced_at":"2025-05-15T17:04:26.123Z","repository":{"id":24036672,"uuid":"27421773","full_name":"rcaloras/bash-preexec","owner":"rcaloras","description":"⚡ preexec and precmd functions for Bash just like Zsh. ","archived":false,"fork":false,"pushed_at":"2025-01-13T02:59:03.000Z","size":111,"stargazers_count":931,"open_issues_count":35,"forks_count":97,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-05-08T11:44:45.926Z","etag":null,"topics":["bash","precmd","preexec"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rcaloras.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-12-02T08:03:08.000Z","updated_at":"2025-05-05T12:50:11.000Z","dependencies_parsed_at":"2024-02-25T20:30:42.530Z","dependency_job_id":"5fc944f7-1ee8-4f1e-9417-71854ff22f1d","html_url":"https://github.com/rcaloras/bash-preexec","commit_stats":{"total_commits":136,"total_committers":27,"mean_commits":5.037037037037037,"dds":0.6691176470588236,"last_synced_commit":"fb23d474b330fac4704b9baa7cfa98745ab1015b"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rcaloras%2Fbash-preexec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rcaloras%2Fbash-preexec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rcaloras%2Fbash-preexec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rcaloras%2Fbash-preexec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rcaloras","download_url":"https://codeload.github.com/rcaloras/bash-preexec/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254384987,"owners_count":22062422,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","precmd","preexec"],"created_at":"2024-08-01T09:00:57.098Z","updated_at":"2025-05-15T17:04:26.092Z","avatar_url":"https://github.com/rcaloras.png","language":"Shell","funding_links":[],"categories":["Shell","Interactive Tools"],"sub_categories":["Reusable Things"],"readme":"[![Build Status](https://github.com/rcaloras/bash-preexec/actions/workflows/bats.yaml/badge.svg)](https://github.com/rcaloras/bash-preexec/actions/)\n[![GitHub version](https://badge.fury.io/gh/rcaloras%2Fbash-preexec.svg)](https://badge.fury.io/gh/rcaloras%2Fbash-preexec)\n\nBash-Preexec \n============\n\n**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).\n\n\u003ca href=\"https://bashhub.com\" target=\"_blank\"\u003e\u003cimg src=\"https://bashhub.com/assets/images/bashhub-logo.png\" alt=\"Bashhub Logo\" width=\"200\"\u003e\u003c/a\u003e\n\nThis project is currently being used in production by [Bashhub](https://github.com/rcaloras/bashhub-client), [iTerm2](https://github.com/gnachman/iTerm2), and [Fig](https://fig.io). Hype!\n\n## Quick Start\n```bash\n# Pull down our file from GitHub and write it to your home directory as a hidden file.\ncurl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh\n# Source our file to bring it into our environment\nsource ~/.bash-preexec.sh\n# Define a couple functions.\npreexec() { echo \"just typed $1\"; }\nprecmd() { echo \"printing the prompt\"; }\n```\n\n## Install\nYou'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.**\n```bash\n# Pull down our file from GitHub and write it to your home directory as a hidden file.\ncurl https://raw.githubusercontent.com/rcaloras/bash-preexec/master/bash-preexec.sh -o ~/.bash-preexec.sh\n# Source our file at the end of our bash profile (e.g. ~/.bashrc, ~/.profile, or ~/.bash_profile)\necho '[[ -f ~/.bash-preexec.sh ]] \u0026\u0026 source ~/.bash-preexec.sh' \u003e\u003e ~/.bashrc\n```\n\n## Usage\nTwo functions **preexec** and **precmd** can now be defined and they'll be automatically invoked by bash-preexec if they exist.\n\n* `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.\n* `precmd` Executed just before each prompt. Equivalent to PROMPT_COMMAND, but more flexible and resilient.\n```bash\nsource ~/.bash-preexec.sh\npreexec() { echo \"just typed $1\"; }\nprecmd() { echo \"printing the prompt\"; }\n```\nShould output something like:\n```\nelementz@Kashmir:~/git/bash-preexec (master)$ ls\njust typed ls\nbash-preexec.sh  README.md  test\nprinting the prompt\n```\n#### Function Arrays\nYou 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.\n* `$preexec_functions` Array of functions invoked by preexec.\n* `$precmd_functions` Array of functions invoked by precmd.\n\n#### preexec\n```bash\n# Define some function to use preexec\npreexec_hello_world() { echo \"You just entered $1\"; }\n# Add it to the array of functions to be invoked each time.\npreexec_functions+=(preexec_hello_world)\n```\n\n#### precmd\n```bash\nprecmd_hello_world() { echo \"This is invoked before the prompt is displayed\"; }\nprecmd_functions+=(precmd_hello_world)\n```\n\nYou can also define multiple functions to be invoked like so.\n\n```bash\nprecmd_hello_one() { echo \"This is invoked on precmd first\"; }\nprecmd_hello_two() { echo \"This is invoked on precmd second\"; }\nprecmd_functions+=(precmd_hello_one)\nprecmd_functions+=(precmd_hello_two)\n```\n\nYou can check the functions set for each by echoing its contents.\n\n```bash\necho ${preexec_functions[@]}\necho ${precmd_functions[@]}\n```\n\n## Subshells\nbash-preexec does not support invoking preexec() for subshells by default. It must be enabled by setting \n`__bp_enable_subshells`.\n```bash\n# Enable experimental subshell support\nexport __bp_enable_subshells=\"true\"\n```\nThis 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)\n\n## Library authors\nIf 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`:\n\n```bash\nif [[ -n \"${bash_preexec_imported:-}\" ]]; then\n    echo \"Bash-preexec is loaded.\"\nfi\n```\n\n## Tests\nYou can run tests using [Bats](https://github.com/bats-core/bats-core).\n```bash\nbats test\n```\nShould output something like:\n```\nelementz@Kashmir:~/git/bash-preexec(master)$ bats test\n ✓ No functions defined for preexec should simply return\n ✓ precmd should execute a function once\n ✓ preexec should execute a function with the last command in our history\n ✓ preexec should execute multiple functions in the order added to their arrays\n ✓ preecmd should execute multiple functions in the order added to their arrays\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frcaloras%2Fbash-preexec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frcaloras%2Fbash-preexec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frcaloras%2Fbash-preexec/lists"}