Search code examples
bashaliasexecutableterraformansible-inventory

How to use an alias by an executable?


When running terraform-inventory executable, in the background it runs another executable by name terraform. I would like it to follow an alias and run terraformX instead. Since I want original terraform executable to remain unchanged, I added terraformX. Therefore I can't create a symlink to terraformX and name it terraform. terraformX is another version of terraform.

Alias command:

alias terraform='terraformX'

You could enable alias expansion for scripts with:

shopt -s expand_aliases

However, this is not a script, and the executable is not expanding the alias. Is there a way to make it expand the alias?

terraform-inventory is an executable from this project: https://github.com/adammck/terraform-inventory.
terraform is an executable from this website: https://www.terraform.io/docs/commands/index.html


Solution

  • Instead of an alias, a temporary modification of PATH environment variable can help. The key is to remove the path of terraform executable and put terraformX executable in a different location (then terraformX can be named terraform or a symlink created to it with name terraform).

    Run:

    which terraform
    

    Output:

    /usr/bin/terraform
    

    Run:

    export PATH=~/:/bin/
    mv terraformX ~/terraform
    which terraform
    

    Output:

    ~/terraform
    

    Now running terraform-inventory --list will run terraform state pull in the background using ~/terraform instead of /usr/bin/terraform.