How To

How To Fix “_get_comp_words_by_ref: command not found”

Press Tab after kubectl, or any other tool whose completion script you sourced, and the shell answers with an error instead of suggestions:

Original content from computingforgeeks.com - post 53152
_get_comp_words_by_ref: command not found

The message looks like the tool is broken. It is not. _get_comp_words_by_ref is a helper function that belongs to the bash-completion project, and the error means your shell loaded a completion script that expects the helper before the helper itself existed. The fix takes one package install plus one source command, but knowing which of the three underlying causes you have saves the guessing. Everything below was reproduced and fixed in August 2026 on an Ubuntu 24.04 base, with Debian and Fedora containers used to confirm the package behavior, using the completion script kubectl generates.

What Actually Causes the Error

Completion scripts do not parse your command line themselves. They call helpers that the bash-completion package defines, and _get_comp_words_by_ref is the one that splits the current line into words. On a system with the package installed, it is an ordinary shell function:

grep -n "^_get_comp_words_by_ref()" /usr/share/bash-completion/bash_completion

The function lives in the main bash_completion file, which your shell sources at startup:

369:_get_comp_words_by_ref()

Generated completion scripts depend on it even when they try not to. The script from kubectl completion bash checks for the bash-completion package first, then falls back to a minimal function of its own, and that fallback still calls the missing helper:

__kubectl_init_completion()
{
    COMPREPLY=()
    _get_comp_words_by_ref "$@" cur prev words cword
}

So the moment Tab triggers that code path on a machine without bash-completion loaded, the error fires. The same applies to scripts generated by helm, oc, and most other Go tools, plus plenty of hand-written completion files. It shows up most often on minimal installs: a fresh Debian container image, for example, does not ship the package at all:

dpkg -l bash-completion

The un state confirms it was never installed:

un  bash-completion <none>       <none>       (no description available)

Install the bash-completion Package

On Ubuntu, Debian, and Linux Mint:

sudo apt install -y bash-completion

On Fedora, RHEL, Rocky Linux, and AlmaLinux the package carries the same name:

sudo dnf install -y bash-completion

Confirm the install registered:

rpm -q bash-completion

The query returns the packaged version:

bash-completion-2.17-2.fc44.noarch

Load the Helpers Without Logging Out

Installing the package drops the files on disk, but the shell you are sitting in has already finished its startup. A fresh login would pick everything up. To fix the current session instead, source the loader directly:

source /etc/profile.d/bash_completion.sh

Then check that the missing function now exists. This one command is the fastest way to tell whether any machine has working completion helpers:

type _get_comp_words_by_ref

Before the fix it reports not found. After sourcing, the answer changes:

_get_comp_words_by_ref is a function

Re-source your tool’s completion script and Tab behaves again. On the test machine, kubectl g followed by Tab expanded to kubectl get immediately after this, with no error. Commands and flags for the tool itself are in our kubectl cheat sheet if that is the workflow you are setting up.

When the Package Is Installed but the Error Persists

The loader in /etc/profile.d/bash_completion.sh refuses to run outside an interactive bash session. It checks for a prompt before sourcing anything:

# Check for interactive bash and that we haven't already been sourced.
if [ "x${BASH_VERSION-}" != x -a "x${PS1-}" != x -a "x${BASH_COMPLETION_VERSINFO-}" = x ]; then

That guard explains the stubborn cases. A script, a cron job, or a one-off ssh host command runs non-interactively, so the helpers never load there, and any completion script such a session sources will throw the error unless the session sources /usr/share/bash-completion/bash_completion itself first. The other common case is a hand-built ~/.bashrc that lost the stock completion block. The distribution default ends with this, and if yours does not, add it back:

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

Note the first line: the stock block refuses to load bash-completion when bash runs in POSIX mode, since the package relies on bashisms, which is worth knowing if your prompt comes from a hardened base image. The same shape of problem, a command that exists as a package but not in your current shell environment, is behind errors like mkvirtualenv: command not found too.

Fix It in Zsh

Zsh users meet a cousin of this error, usually with extra noise, because bash completion scripts are not valid zsh out of the box. Sourcing one in a plain zsh session fails before Tab is ever pressed:

/proc/self/fd/12:type:434: bad option: -t
/proc/self/fd/12:437: command not found: complete

The often-suggested workaround is zsh’s bash compatibility layer, and it is worth understanding exactly what it does and does not give you:

autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit

bashcompinit provides complete and compgen as shell functions that emulate the bash builtins, so simple self-contained bash completion scripts start working. It does NOT provide the bash-completion project’s helper functions. In testing, a script that calls _get_comp_words_by_ref kept failing under bashcompinit, because that function comes from the bash-completion package, which zsh never loads. For any tool that generates its own completions, the real fix is the native zsh output:

source <(kubectl completion zsh)

That loaded cleanly in testing and registers a proper _kubectl completion function, no compatibility layer involved. Check whether your tool has a completion zsh subcommand before reaching for bashcompinit. There is a fuller walkthrough of the compatibility route in enabling bash completion scripts in zsh, and if you are building out a zsh setup anyway, Oh My Zsh with command autosuggestions ships sane completion defaults that avoid this class of error entirely.

One habit closes this whole topic: whenever a completion script misbehaves on any machine, run type _get_comp_words_by_ref first. The answer tells you in one line whether you are missing the package, missing the sourcing, or running in a shell that will never load it.

Keep reading

Fix “apt-key is deprecated. Manage keyring files in trusted.gpg.d instead” Debian Fix “apt-key is deprecated. Manage keyring files in trusted.gpg.d instead” Best Terminal Shell Prompts for Zsh, Bash and Fish Featured Best Terminal Shell Prompts for Zsh, Bash and Fish How To Install Zsh and Oh My Zsh on macOS macos How To Install Zsh and Oh My Zsh on macOS Install Zsh and Oh My Zsh on Linux AlmaLinux Install Zsh and Oh My Zsh on Linux Install OpenStack CLI Client on Ubuntu / Debian / RHEL Cloud Install OpenStack CLI Client on Ubuntu / Debian / RHEL Install Windows Terminal on Windows 10 | Server 2019 Terminal Install Windows Terminal on Windows 10 | Server 2019

Leave a Comment

Press ESC to close