# Cant get jq to work in pipeline script

**URL:** <https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408>\
**Category:** Ask a question\
**Created:** [May 13, 2022, 5:58pm UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408 "2022-05-13T17:58:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jordan.kasper](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/jordan.kasper/32/1510_2.png) [@jordan.kasper](https://community.jenkins.io/u/jordan.kasper)\
**Post date:** [May 13, 2022, 5:58pm UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/1 "2022-05-13T17:58:41Z")

</div>

Hello All,

Trying to do something I hope should be pretty simple. I am running a simple pipeline script and in the build stage trying to run

`labId=$(cat slmaven.json |jq -r '.labId')`

But I keep getting

```auto
/Users/jordankasper/.jenkins/workspace/env_pipeline@tmp/durable-a197f770/script.sh: line 3: jq: command not found

```

I installed Jenkins on my mac and followed those install instructions but cant find a way for my jenkins instance to see jq on my local machine.

What do I need to do so it can be seen my Jenkins when it runs the pipeline script?

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [May 13, 2022, 7:50pm UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/2 "2022-05-13T19:50:06Z")

</div>

In that script, output the value of the `PATH` environment variable. You’ll probably find that when running in the Jenkins agent, it does not include `/usr/local/bin` (or where ever your copy of `jq`) is stored.

Extend the script to assign `PATH=$PATH:/usr/local/bin` and try it again.

---

<div class="post-metadata">

**Author:** ![jordan.kasper](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/jordan.kasper/32/1510_2.png) [@jordan.kasper](https://community.jenkins.io/u/jordan.kasper)\
**Post date:** [May 13, 2022, 8:37pm UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/3 "2022-05-13T20:37:04Z")

</div>

@MarkEWaite this is what I get when I echo out the PATH

```auto
PATH=/Users/jordankasper/.jenkins/tools/hudson.model.JDK/1.8/bin:/Users/jordankasper/.jenkins/tools/hudson.tasks.Maven_MavenInstallation/3.8.1/bin:/Users/jordankasper/.jenkins/tools/hudson.model.JDK/1.8/bin:/Users/jordankasper/.jenkins/tools/hudson.tasks.Maven_MavenInstallation/3.8.1/bin:/usr/local/bin/jq:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

```

If I do a which jq I get

```auto
/usr/local/bin/jq

```

Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [May 13, 2022, 8:45pm UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/4 "2022-05-13T20:45:13Z")

</div>

If your agent is running directly on the computer, then it should “just work”.

```nohighlight
pipeline {
    agent {
        label '!windows'
    }
    stages {
        stage('Example') {
            steps {
                sh 'echo PATH is $PATH && which jq'
            }
        }
    }
}

```

In my case, I don’t have `jq` installed, so the output of the `which` command is empty.

---

<div class="post-metadata">

**Author:** ![jordan.kasper](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/jordan.kasper/32/1510_2.png) [@jordan.kasper](https://community.jenkins.io/u/jordan.kasper)\
**Post date:** [May 13, 2022, 9:06pm UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/5 "2022-05-13T21:06:14Z")

</div>

@MarkEWaite I am still not sure what I am doing wrong.

Here is the code

```auto
pipeline {
    agent {
        label '!windows'
    }
    stages {
        stage('Example') {
            steps {
                sh 'echo PATH is $PATH && PATH=$PATH:/usr/local/bin/jq'
                sh 'echo "{ "foo": 123, "bar": 456 }" | jq ".foo"'
                
            }
        }
    }
}

```

Here is the output

```auto
+ echo PATH is /usr/bin:/bin:/usr/sbin:/sbin
PATH is /usr/bin:/bin:/usr/sbin:/sbin
+ PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin/jq
[Pipeline] sh
+ echo '{ foo: 123, bar: 456 }'
+ jq .foo
/Users/jordankasper/.jenkins/workspace/jq_test@tmp/durable-25b496ab/script.sh: line 1: jq: command not found

```

Im assuming Im still not setting it right but not sure how maybe.

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [May 13, 2022, 10:38pm UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/6 "2022-05-13T22:38:59Z")

</div>

That’s very close. You need to add the directory to the path, not the program. You need to handle the arguments to echo with consistent quoting.

> [@jordan.kasper](#):
>
> ```auto
> pipeline {
> agent {
> label '!windows'
> }
> stages {
> stage('Example') {
> steps {
> sh '''
> echo PATH before is $PATH
> PATH=$PATH:/usr/local/bin
> echo PATH after is $PATH
> echo "{ 'foo': 123, 'bar': 456 }" | jq ".foo"
> '''
> }
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![jordan.kasper](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/jordan.kasper/32/1510_2.png) [@jordan.kasper](https://community.jenkins.io/u/jordan.kasper)\
**Post date:** [May 13, 2022, 11:03pm UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/7 "2022-05-13T23:03:43Z")

</div>

@MarkEWaite still getting the same thing

```auto
+ echo PATH before is /usr/bin:/bin:/usr/sbin:/sbin
PATH before is /usr/bin:/bin:/usr/sbin:/sbin
+ PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
+ echo PATH after is /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
PATH after is /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
[Pipeline] sh
+ echo '{ foo: 123, bar: 456 }'
+ jq .foo
/Users/jordankasper/.jenkins/workspace/jq_test@tmp/durable-2fde5e04/script.sh: line 1: jq: command not found

```

so on my mac if I do a which jq it gives me

```auto
/usr/local/bin/jq

```

Which means I should have to append with just

```auto
/usr/local/bin/

```

Which per the output above is what I have done. Is there anything else that could be hold it up?

This is the code I am running, syntax seems correct.

```auto
pipeline {
    agent {
        label '!windows'
    }
    stages {
        stage('Example') {
            steps {
                sh '''
                    echo PATH before is $PATH
                    PATH=$PATH:/usr/local/bin
                    echo PATH after is $PATH
                '''
                sh 'echo "{ "foo": 123, "bar": 456 }" | jq ".foo"'
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![halkeye](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/halkeye/32/8_2.png) [@halkeye](https://community.jenkins.io/u/halkeye)\
**Post date:** [May 14, 2022, 12:22am UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/8 "2022-05-14T00:22:09Z")

</div>

> [@jordan.kasper](#):
>
> ```auto
> sh '''
> echo PATH before is $PATH
> PATH=$PATH:/usr/local/bin
> echo PATH after is $PATH
> '''
> 
> ```

Each sh statement has its own env. So setting it in one won’t affect other ones.  
You could combine them into a single step.  
You could also be explicit and just use `sh 'echo "{ "foo": 123, "bar": 456 }" | /usr/local/bin/jq ".foo"'`

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [May 14, 2022, 12:37am UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/9 "2022-05-14T00:37:32Z")

</div>

> [@halkeye](#):
>
> You could also be explicit and just use `sh 'echo "{ "foo": 123, "bar": 456 }" | /usr/local/bin/jq ".foo"'`

If `/usr/local/bin/jq` also reports `command not found` then you may want to check that the file stored at `/usr/local/bin/jq` is the correct executable type for your computer. I think that I’ve seen `command not found` when an executable was not what my system expected.

---

<div class="post-metadata">

**Author:** ![jordan.kasper](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/jordan.kasper/32/1510_2.png) [@jordan.kasper](https://community.jenkins.io/u/jordan.kasper)\
**Post date:** [May 14, 2022, 4:08pm UTC](https://community.jenkins.io/t/cant-get-jq-to-work-in-pipeline-script/2408/10 "2022-05-14T16:08:19Z")

</div>

Thank you @halkeye and @MarkEWaite this worked great!
