Created
October 28, 2025 06:35
-
-
Save BradenM/06012c0074d0d2001b07df8d9d25f54c to your computer and use it in GitHub Desktop.
Direnv extended export_function helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Export a function in direnv environment. | |
| # Extension of: https://github.com/direnv/direnv/issues/73#issuecomment-152284914 | |
| # with support for specifying execution shell | |
| export_function() { | |
| local name=$1 | |
| local sh="${2:-$SHELL}" | |
| local alias_dir="$(direnv_layout_dir)/aliases" | |
| mkdir -p "$alias_dir" | |
| PATH_add "$alias_dir" | |
| local target="$alias_dir/$name" | |
| if declare -f "$name" >/dev/null; then | |
| #echo "#!$SHELL" >"$target" | |
| echo "#!/usr/bin/env $(basename "$sh")" >"$target" | |
| declare -f "$name" >>"$target" 2>/dev/null | |
| # Notice that we add shell variables to the function trigger. | |
| echo "$name \$*" >>"$target" | |
| chmod +x "$target" | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment