Reveal the command behind an alias with ZSH
Tags: productivity, bash, tutorial, terminal
Published: 2021-02-22 15:33:50

I love alias commands, but also aalias_forlways like to 🕵️‍♀️ spy on what is the real command behind an alias. There are a lot of ways to see it:

- Using the `alias <command>` on terminal
- Using the keyboard shortcut combination `ctrl+x a` (not `ctrl+x+a`) to expand it
- Using the [globalias](https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/globalias) plugin

But all of them need an extra step, and I found a way to see it always before the command runs.

# Reveal

We can use a function to search the command associated with an alias adding this script on `~/.zshrc` file.

```bash
local cmd_alias=""

# Reveal Executed Alias
alias_for() {
  [[ $1 =~ '[[:punct:]]' ]] && return
  local search=${1}
  local found="$( alias $search )"
  if [[ -n $found ]]; then
    found=${found//\\//} # Replace backslash with slash
    found=${found%\'} # Remove end single quote
    found=${found#"$search="} # Remove alias name
    found=${found#"'"} # Remove first single quote
    echo "${found} ${2}" | xargs # Return found value (with parameters)
  else
    echo ""
  fi
}

expand_command_line() {
  first=$(echo "$1" | awk '{print $1;}')
  rest=$(echo ${${1}/"${first}"/})

  if [[ -n "${first//-//}" ]]; then # is not hypen
    cmd_alias="$(alias_for "${first}" "${rest:1}")" # Check if there's an alias for the command
    if [[ -n $cmd_alias ]] && [[ "${cmd_alias:0:1}" != "." ]]; then # If there was and not start with dot
      echo "${T_GREEN}❯ ${T_YELLOW}${cmd_alias}${F_RESET}" # Print it
    fi
  fi
}

pre_validation() {
  [[ $# -eq 0 ]] && return                    # If there's no input, return. Else...
  expand_command_line "$@"
}
```

Now we need to run this function every time the alias is entered. Happily, ZSH has a [preexec](http://zsh.sourceforge.net/Doc/Release/Functions.html) function that can be associated with a hook. Just add this script after the previous on `~/.zshrc` file.

```bash
autoload -U add-zsh-hook                      # Load the zsh hook module. 
add-zsh-hook preexec pre_validation           # Adds the hook 
```

I'm not planning to remove this hook, but you can do it with this command:

```bash
add-zsh-hook -d preexec pre_validation        # Remove it for this hook.
```

Once finish, reopen all terminals or update his source running `source ~/.zshrc` command and now you can see those secret commands.

# Example

![alias](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27sukg7wdvfmyjaxuvye.png)

**Yellow:** alias
**Red:** command behind revealed

---

**Sources**: 

- [Stackoverflow - Print executed alias](https://stackoverflow.com/questions/18143307/print-executed-alias-in-zsh-or-bash/18370868#comment116396914_18370868)

- [Stackoverflow - Expand alias command](https://superuser.com/questions/1514569/how-to-expand-aliases-inline-in-zsh#:~:text=Just%20press%20C%2Dx%20a%20%2C%20not,function%20to%20expand%20the%20alias.)

- [Bash Colors](https://misc.flogisoft.com/bash/tip_colors_and_formatting)

---

You can download or clone this code and other ZSH utilities from GitHub: [dot Files](https://github.com/deinsoftware/dot-files) repository.

---

**That’s All Folks!**
**Happy Coding** 🖖

[![beer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkgsx4aw6qn9i9ax6as9.png)](https://github.com/sponsors/deinsoftware)