1

I'm trying to figure out the best way to create inline expansions of fragments of commands. The "long form" would be like:

kubectl get pods --show-labels

or

 kubectl get po -o custom-columns=NAME:'{.metadata.name}',OWNER:'{.metadata.ownerReferences[0].name}',OWNER_KIND:'{.metadata. ownerReferences[0].kind}'

For the first, I have used this:

function sl(){
  echo " --show-labels"
}

  kubectl get pods `sl`

This works, but the following does not:

function kown(){
  echo "custom-columns=NAME:'{.metadata.name}',OWNER:'{.metadata.ownerReferences[0].name}',OWNER_KIND:'{.metadata.ownerReferences[0].kind}'"
}

kubectl get po -o `kown`

error: unexpected path string, expected a 'name1.name2' or '.name1.name2' or '{name1.name2}' or '{.name1.name2}'

Why is the second one not working? Is there a better way to do this rather than backticks and functions? I was thinking env variables but not sure. Thx for any ideas.

1 Answer 1

2

First off, in zsh, it's easier to use global aliases for this sort of thing:

alias -g sl='--show-labels'
alias -g kown='custom-columns=NAME:{.metadata.name},OWNER:{.metadata.ownerReferences[0].name},OWNER_KIND:{.metadata. ownerReferences[0].kind}'

Secondly, if you use 'single quotes' inside "double quotes", then the single quotes are included literally in the string you pass to your command. That's not what you want. The single quotes in the original command line were there to prevent the shell from doing expansion on several of the string's substrings. Note how instead I've wrapped the entire string in single quotes. This prevents the shell from doing any expansion on the string, anywhere in the string. This is much safer than quoting only some of the substrings (and less typing, too).

Finally, you use global aliases simply by typing them anywhere on the command line, without quotes:

% kubectl get pods sl
% kubectl get po -o kown

Note: % is the prompt. Don't type that. 🙂


Some additional notes:

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.