Skip to content

Instantly share code, notes, and snippets.

@gh640
Created May 14, 2022 04:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gh640/52abf6730c9a4136232ea7ef00a73a86 to your computer and use it in GitHub Desktop.
Save gh640/52abf6730c9a4136232ea7ef00a73a86 to your computer and use it in GitHub Desktop.
Zsh / Bash: Getting the current function name invoked
function func_name {
case "${SHELL}" in
*/bash)
# Bash
echo ${FUNCNAME[2]}
;;
*/zsh)
# Zsh
echo ${funcstack[2]}
;;
*)
echo "Unsupported shell: ${SHELL}."
esac
}
@gh640
Copy link
Author

gh640 commented May 14, 2022

This function works only inside another function.

Examples:

function tar_compress {
  if [ -z "${1}" ]; then
    echo "usage: $(func_name) DIR_NAME"
    return 1
  fi

  tar -czvf "${1}.tar.gz" "${1}"
}
function tar_extract {
  if [ -z "${1}" ]; then
    echo "usage: $(func_name) TAR_FILENAME"
    return 1
  fi

  tar -zxvf
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment