Kubernetes

Enable Kubectl Bash Completion

The cmd.exe shell service for windows does not allow auto-completion while typing. However, through the Linux subsystem in windows, we can use a Linux terminal commonly known as bash completion. The Linux subsystem for windows can replace the windows PowerShell and cmd.exe while having complete access to Windows files and folders.

The working of bash completion is to automatically complete the commands without the user typing in every bit of the command. The user just has to start the command they want to use and just press enter; the bash completion will automatically complete the command script while the user is still typing.

Kubectl Bash Completion

Kubernetes users know that the kubectl command-line tool is an awesome thing. To make it more awesome, you can just enable bash completion. Therefore, when you download kubectl, immediately install the bash completion for your operating system. It will help you just tab-complete all the Pod names with postfix hash.

So let us configure the bash completion. In this article, we will cover the detailed working of bash completion, how to download and install it, and then we will help you configure it as well. Moreover, we will discuss how bash completion can save your time and convenience and how you do not have to remember all the commands. Sounds amazing, right? So, let us begin with installing the bash completion.

Wait! Have you prepared your system for Kubernetes yet? No?

Let us prepare the operating system for Kubernetes first and then configure bash completion for auto-completing all the commands.

Prerequisite:

In order to work with Kubernetes, you need to have an Ubuntu server. We are working with Ubuntu version 20.04, so you just need to get that. Install Ubuntu 20.04 server into your operating system; you can easily download it online from a trusted server. After installing the Ubuntu 20.04 server, log in to your server and start the minikube.

We do this by pressing “Ctrl+Alt+T” on the keyboard. We type the command “minikube start ” into the terminal and then wait a few moments for it to start. Underneath, you can comprehend the output of the ‘start minikube’ command.

Now that your server is ready let us see the functioning of bash completion.

Bash Completion Script

The bash completion script of kubectl can be generated with the following command:

The ‘kubectl completion bash’ command enables the auto-completion of the kubectl script. Sourcing the completion script, you need to install bash completion first. Here is the output of the above command:

However, before you install the bash completion, check whether your server already has it or not. Type the ‘type _init_completion’ command and test you have already installed bash completion.

After executing the ‘type _init_completion’ command, you will get to see the following output:

Install Bash Completion

Now, let’s see the installation process of bash completion. Many package managers provide bash completion; however, you can easily download it with the ‘sudo apt-get install bash completion command.

When you execute the ‘sudo apt-get install bash completion’ command, it will create the main script of bash completion, as shown in the output below.

Keep in mind that you need to manually write this bash completion file in your ~/.bashrc file. However, the bash completion file location might vary from package manager to package manager. You can just type in the ‘type _init_completion’ command to make it simple and easier. Just reload your shell and execute this command.

It will benefit you to know the bash completion status. If the ‘type _init_completion’ command is completed successfully, you are good to go; if not, you need to manually add it to your ~/.bashrc file.

The same output indicates and verifies that your server has successfully installed bash completion.

Enable Kubectl Autocompletion

Now that you have installed bash completion, let us enable it and see it’s working. The first thing that you need to do is to ensure that the kubectl bash completion script has been sourced in all of your shell sessions. You can follow one of the two ways in order to achieve that.

The first way can be achieved through the USER view:

USER

Type ‘echo ‘source <(kubectl completion bash)’ >>~/.bashrc’ command in your command line tool, and you are done.

The second way is to access the system

System

In this mode, you need to type ‘kubectl completion bash | sudo tee /etc/bash_completion.d /kubectl > /dev/null’ command in your command line tool.

Here is a good thing that you would love to know. You can extend the shell completion in both cases by using an alias for kubectl. As both approaches are equivalent, you can use the same commands for each.

Just reload your shell in order to see how the bash completion works.

Conclusion:

The kubectl bash completion for the Ubuntu command line shell allows you to build your commands without typing complete commands quickly. It benefits you to save your time as well as you do not need to remember every command completely. The Kubernetes official documentation provides complete instructions on how to set up bash completion quickly and easily for your development environment. However, in this article, we have covered how to download, install, and configure bash completion. This step-by-step guide will help you configure bash completion into your shell, and you will be able to use it for kubectl autocompleting commands.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content