Search code examples
jsonshellcommandkubectl

Pipe command output inside other command


I'm trying to pipe the output of the first command into the quotation marks at the end of the second command.

kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password}

kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"password":"    Output from command 1    "}}'

I've already tried this: piping output into middle of bash command, but I get the following error with this command:

kubectl --context foo -n foo patch secret postgres.foo-db.credentials -p '{"data":{"`password":"kubectl --context foo -n foo get secret postgres.foo-db.credentials -o jsonpath={.data.password}`"}}'

The request is invalid: patch: Invalid value: "map[data:map[`password:kubectl --context foo  -n foo  get secret postgres.foo-db.credentials -o jsonpath={.data.password}`]]": error decoding from json: illegal base64 data at input byte 7

Solution

  • You can split the command in two to make it easier to understand and let the bash shell expand the variable pgsecret in the second line:

    pgsecret="$(kubectl --context foo -n foo get secret \
      postgres.foo-db.credentials -o jsonpath={.data.password})"
    kubectl \
      --context foo -n foo patch secret \
      postgres.foo-db.credentials -p '{"data":{"password":"'$pgsecret'"}}'