Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

bash problem with single quotes expansion

I got the following script:

#!/usr/bin/env bash

# break on error
set -ex

## USAGE: ./open <type> <instance-name>
if [[ $# -ne 2 ]]; then
    echo "## USAGE: ${0} <type> <instance-name>"
    echo "type: team or project"
    echo "instance-name: name of the instance to fetch the URL from"
    exit 1
fi

ORGANIZATION="redacted_org"

PROJECT="redacted_project"

TYPE="redacted_type"
if [[ $1 == "team" ]]; then
  TYPE="redacted_type_team"
fi

NAME=$2

BUILD_ID=$(az pipelines build definition list \
  --organization "${ORGANIZATION}" \
  --project "${PROJECT}" \
  --query "\"[? contains(path, '\\\\stacks\\\\${TYPE}\\\\instances\\\\${NAME}') && name=='apply'].{id:id}\"" \
  --output tsv)

echo "https://redacted_org.visualstudio.com/redacted_project/_build?definitionId=${BUILD_ID}"

The problem is that the BUILD_ID variable is being executed as (printed by the set -x):

++ az pipelines build definition list --organization redacted_org --project redacted_project --query '"[? contains(path, \'\''\\stacks\\redacted_type\\instances\\redacted_2\'\'') && name==\'\''apply\'\''].{id:id}"' --output tsv

Note the '" instead of " just after the –query parameter and in the end of it, and the \'\' everywhere I have a single quote instead of '

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I need this BUILD_ID to be executed as follows, this command works fine when typed in the terminal itself:

$(az pipelines build definition list --organization "redacted_org" --project "redacted_project" --query "[? contains(path, '\\stacks\\redacted_type\\instances\\redacted_2') && name=='apply'].{id:id}" --output tsv)

What am I doing wrong?

>Solution :

In the stanza:

BUILD_ID=$(az pipelines build definition list \
  --organization "${ORGANIZATION}" \
  --project "${PROJECT}" \
  --query "\"[? contains(path, \'\\\\stacks\\\\${TYPE}\\\\instances\\\\${NAME}\') && name==\'apply\'].{id:id}\"" \
  --output tsv)

you’ve used some bizarre quoting. You want that to be just the way you expect:

BUILD_ID=$(az pipelines build definition list \
  --organization "${ORGANIZATION}" \
  --project "${PROJECT}" \
  --query "[? contains(path, '\\stacks\\${TYPE}\\instances\\${NAME}) && name=='apply'].{id:id}" \
  --output tsv)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading