1

I would very much like to understand how Linux shell hooks work... (bash in particular) Could any one point me to the right documentation or explain the main idea behind this concept?

I've looked into the direnv documentation that states the following

For direnv to work properly it needs to be hooked into the shell. Each shell has its own extension mechanism. Once the hook is configured, restart your shell for direnv to be activated.

I searched through the bash manual tying to find something related to hooking, without success. Is there a blog, tutorial or some other piece of information on how the shell could be extended?

mishoo78
  • 85
  • 2
  • 7
  • 2
    It's nothing that special really. Here's the "hook" for Bash: https://direnv.net/docs/hook.html#bash Start from there. Execute `direnv hook bash` in a terminal and explore its output. – Ionuț G. Stan Apr 28 '22 at 17:43
  • 1
    Most modern shells have a mechanism to specify a command to run before a prompt is printed, or after a prior command is run, etc. – Charles Duffy Apr 28 '22 at 17:43
  • ...there are fancier ways to extend bash -- look at the DEBUG trap if you want a particularly powerful and hard-to-use interface -- but for this kind of usage mode they're overkill. – Charles Duffy Apr 28 '22 at 17:44
  • thank you @CharlesDuffy. browsing through the codebase i found that the hook method actually updates the $PROMPT_COMMAND env var. – mishoo78 Apr 28 '22 at 18:02
  • is there a good tutorial on how to extend bash? – mishoo78 Apr 28 '22 at 18:06
  • Not really, because to "extend bash" is not just one thing; the details depend on what you're trying to accomplish. – Charles Duffy Apr 28 '22 at 19:27
  • hello @CharlesDuffy, so i am really trying to get this. the most basic method of extending some functionality is to provide hooks ( some people would call them life cycle events ) for plugins developed by other people. these plugins will spring into action when the handler for the appropriate hook is executed. when i'm thinking "extending bash" i'm imagining the same scenario, but it seems that, at least for this discussion (hoocking direnv) extending the bash consists of adding some code to a environment variable. i am new to bash scripting, and have little idea of what is possible. – mishoo78 May 04 '22 at 21:43
  • this is i why I am asking, again, to all of you that have more experience in this field, for a good introductory tutorial on this topic. how bash can be extended, what kind of extensions may be created, what best practices should be used for guidance. – mishoo78 May 04 '22 at 21:45
  • The major mechanisms have already been discussed -- DEBUG traps, `PROMPT_COMMAND`. Both of these are covered in `man bash`, as are exported functions (often a convenient way to override a given command's default behavior). There are other, more esoteric things one might also consider extensions, _also_ covered in the man page -- loadable extensions (search for `BASH_LOADABLES_PATH` or `enable`), and sometimes one might get into readline configuration details. – Charles Duffy May 04 '22 at 22:06
  • ...but really, there's not a lot of need for fancy/hidden/nonobvious extensions. `PROMPT_COMMAND` and shell functions cover an overwhelming majority of use cases on their own. – Charles Duffy May 04 '22 at 22:07

0 Answers0