Last active
April 22, 2021 22:09
-
-
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
This file contains 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
#!/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