Skip to content

Instantly share code, notes, and snippets.

@nasamuffin
Last active April 22, 2021 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasamuffin/34feadd5d4a2b99f56a6663c4ff5cb8a to your computer and use it in GitHub Desktop.
Save nasamuffin/34feadd5d4a2b99f56a6663c4ff5cb8a to your computer and use it in GitHub Desktop.
`type` but leveled up - cat the contents of aliases which point to scripts
#!/bin/bash
#
# Usage: 'source /path/to/wat.sh <cmd>'
#
# Outputs 'type <cmd>' or, if it's an alias, cats the script it's aliased to.
# Needs to be sourced, not run, because it needs access to all current shell
# aliases/variables.
WAT_ALIAS_PAT=
WAT_ALIAS_PAT+="("
WAT_ALIAS_PAT+="(?<=is aliased to \`)"
WAT_ALIAS_PAT+="|"
WAT_ALIAS_PAT+="(?<=is aliased to \`source )"
WAT_ALIAS_PAT+=")"
WAT_ALIAS_PAT+="[~/].*"
WAT_ALIAS_PAT+="(?=')"
WAT_TYPE_OUT="$(type "$@")"
WAT_TYPE_GREP="$(grep -oP "${WAT_ALIAS_PAT}" <<<"${WAT_TYPE_OUT}")"
echo "${WAT_TYPE_OUT}"
if [[ "${WAT_TYPE_GREP}" ]]; then
cat "${WAT_TYPE_GREP/#\~/$HOME}"
fi
# Since this script is designed to be sourced, not invoked in a subshell, clean
# up after ourselves.
WAT_ALIAS_PATH=
WAT_TYPE_GREP=
WAT_TYPE_OUT=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment