diff --git a/autotools/build-bash-completion b/autotools/build-bash-completion
index c94ad261b0d3aa4789781c04d5981fd88dad9556..0d03cf83c7d3654dd30c3694574cd64628179367 100755
--- a/autotools/build-bash-completion
+++ b/autotools/build-bash-completion
@@ -236,10 +236,20 @@ def WritePreamble(sw):
     sw.DecIndent()
   sw.Write("}")
 
+  # Params: <compgen options>
+  # Result variable: $COMPREPLY
+  sw.Write("_ganeti_compgen() {")
+  sw.IncIndent()
+  try:
+    sw.Write("""COMPREPLY=( $(compgen "$@") )""")
+    sw.Write("_ganeti_dbglog COMPREPLY=\"${COMPREPLY[@]}\"")
+  finally:
+    sw.DecIndent()
+  sw.Write("}")
+
 
 def WriteCompReply(sw, args, cur="\"$cur\""):
-  sw.Write("""COMPREPLY=( $(compgen %s -- %s) )""", args, cur)
-  sw.Write("_ganeti_dbglog COMPREPLY=\"${COMPREPLY[@]}\"")
+  sw.Write("_ganeti_compgen %s -- %s", args, cur)
   sw.Write("return")