Skip to content

Instantly share code, notes, and snippets.

@BradenM
Created October 28, 2025 06:35
Show Gist options
  • Select an option

  • Save BradenM/06012c0074d0d2001b07df8d9d25f54c to your computer and use it in GitHub Desktop.

Select an option

Save BradenM/06012c0074d0d2001b07df8d9d25f54c to your computer and use it in GitHub Desktop.
Direnv extended export_function helper
#!/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