kuberneteszshkubectloh-my-zshzshrc

oh-my-zsh fast dry-run=client -o yaml for kubectl


I am using zsh with oh-my-zsh and I put an entry into my ~/.zshrc to have a shortcut for the option --dry-run=client -o yaml as a variable and be faster for generating yaml files with imperative commands when I enter for example kubectl run test-pod --image=nginx $do I get the error error: Invalid dry-run value (client -o yaml). Must be "none", "server", or "client". as if the equal operator has not been read. it works fine with bash

I am using kubectl plugin for auto-completion

my zshrc:

plugins=(git docker kubectl docker-compose ansible zsh-autosuggestions zsh-syntax-highlighting sudo terraform zsh-completions)


alias ls="exa --icons --group-directories-first"
alias ls -l="exa --icons --group-directories-first -lg"
alias ll="exa --icons --group-directories-first -lg"
alias cat="ccat --bg=dark -G Plaintext=brown -G Punctuation=white"

export PATH=$PATH:/usr/local/go/bin

# create yaml on-the-fly faster
export do='--dry-run=client -o yaml'

my bashrc:

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
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

export do='--dry-run=client -o yaml'

and when I execute the command it works fine

$ kubectl run test-pod --image=nginx $do
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: test-pod
  name: test-pod
spec:
  containers:
  - image: nginx
    name: test-pod
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

Solution

  • As @Gairfowl metioned The distinction is that word splitting for unquoted parameter expansions is not performed by zsh (by default).

    By enabling the SH WORD SPLIT option or by using the = flag on a specific expansion, you can enable "regular" word splitting same as bash. To do that you need to follow this syntax

    kubectl run test-pod --image=nginx ${=do}
    

    or

    kubectl run test-pod --image=nginx ${do}
    

    In case if these two dont work try to use the setopt

    setopt SH_WORD_SPLIT
    kubectl run test-pod --image=nginx $do