Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.3K
Visualizações
jq with single quotes bash script

I tried to use bash to run the following script:

#!/bin/bash

myvar="data1"
data='[
  {
    "resource_name": "data1.something",
    "resource_type": "Topic"
  },
  {
    "resource_name": "data2.something",
    "resource_type": "Topic"
  }
]'
query=$(echo ".[] | select((.resource_type==\"Topic\") and (.resource_name | startswith(\"${myvar}\") | not))")
echo ${data} | jq ${query}

It doesn't work becasue of line :

echo ${data} | jq ${query}

But if I run the same script in zsh, it works. and gives me the following error:

jq: error: Could not open file |: No such file or directory

jq: error: Could not open file select((.resource_type=="Topic"): No such file or directory
jq: error: Could not open file and: No such file or directory
jq: error: Could not open file (.resource_name: No such file or directory
jq: error: Could not open file |: No such file or directory
jq: error: Could not open file startswith("data1"): No such file or directory
jq: error: Could not open file |: No such file or directory
jq: error: Could not open file not)): No such file or directory

I was unable to understand what exactly is the issue here, I can only think that I somehow need to add single quotes when using with bash.

For example, if I use single quotes:

echo ${data} | jq \'${query}\'

it gives an error:

jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:

'.[] jq: 1 compile error

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Using the --arg option to import content as variables is preferred over injecting shell variables into the actual filter code. This also saves you from dealing with single or double quotes.

#!/bin/bash

myvar="data1"
data='[
  {
    "resource_name": "data1.something",
    "resource_type": "Topic"
  },
  {
    "resource_name": "data2.something",
    "resource_type": "Topic"
  }
]'
query='.[] | select((.resource_type==$type) and (.resource_name | startswith($var) | not))'
echo "${data}" | jq --arg type "Topic" --arg var "${myvar}" "${query}"
{
  "resource_name": "data2.something",
  "resource_type": "Topic"
}

Demo

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda