Setting up Bash Completion for Azure CLI on a Mac with zsh

Tools

I’ve been increasingly using the Azure CLI for most of my day to day tasks in Azure. Whilst the portal is good for working things out, I find it painfully slow when I know what I want to do. The Azure CLI lets me get things done faster, but it is very extensive and can be intimidating to learn. After updating the CLI and seeing, once again, that bash completion was available, I decided to finally get it setup. At the time of writing, my setup is as follows:

  • macOS Catalina 10.15.5
  • Z Shell (zsh) bundled with macOS (version 5.7.1)
  • azure-cli 2.8.0

There were a number of resources I found on setting up Bash completion (see the Sources section below). The steps I needed to perform ended up being a mix of the two, but quite simple.

The Quick Fix

Simply modify your .zshrc file and add the following two lines at the end of the file.

autoload bashcompinit && bashcompinit
source /usr/local/etc/bash_completion.d/az

Save and reload your zsh profile with source .zshrc or by restarting your terminal. To test, type az acc and hit ‘tab’. If completions are setup correctly the command az account will be autocompleted for you.

Finding the Location of azure-cli bash completions

My azure-cli bash completions where installed at source /usr/local/etc/bash_completion.d/az. This was easy enough to determine. After an Azure CLI update via homebrew, you will notice the following message:

==> azure-cli
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

A quick ls /usr/local/etc/bash_completion.d revealed

az            brew          brew-services npm           prlctl        prlsrvctl     subversion

Since the Azure CLI command is “az” that is the completion to choose.

Sources

https://github.com/Azure/azure-cli/issues/1722 https://stackoverflow.com/questions/49273395/how-to-enable-command-completion-for-azure-cli-in-zsh