Home > OS >  bash problem with single quotes expansion
bash problem with single quotes expansion

Time:01-29

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 '

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?

CodePudding user response:

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)
  •  Tags:  
  • Related