# Exec Node with Variables

**URL:** <https://discourse.nodered.org/t/exec-node-with-variables/77089>\
**Category:** General\
**Created:** [30 March 2023 19:39 UTC](https://discourse.nodered.org/t/exec-node-with-variables/77089 "2023-03-30T19:39:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorccan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lorccan](https://discourse.nodered.org/u/lorccan)\
**Post date:** [30 March 2023 19:39 UTC](https://discourse.nodered.org/t/exec-node-with-variables/77089/1 "2023-03-30T19:39:50Z")

</div>

I'm able to execute this: `/usr/bin/avahi-browse -t _hap._tcp | grep "03V8" | wc -l` at the command line and I can also feed it to an Exec node [with Append msg.payload checked] to get the same output.

The value being grepped for, "03V8", identifies one of several devices, so I'd like to find a way that I can use a variable or template that I can update independently.

Is this possible please?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 March 2023 19:53 UTC](https://discourse.nodered.org/t/exec-node-with-variables/77089/2 "2023-03-30T19:53:27Z")

</div>

Hi @lorccan

You could make use of a subflow?  
passing in only the "03V8" value to it?

```auto
[{"id":"793c0c2e9751236e","type":"subflow","name":"avahi-browse","info":"","category":"","in":[{"x":155,"y":195,"wires":[{"id":"a85816385af348e7"}]}],"out":[{"x":770,"y":180,"wires":[{"id":"d13e98cb53d509d6","port":0}]}],"env":[],"meta":{},"color":"#3FADB5","icon":"node-red-contrib-tplink-tapo-connect-api/icon-command.png"},{"id":"d13e98cb53d509d6","type":"exec","z":"793c0c2e9751236e","command":"/usr/bin/avahi-browse","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":560,"y":195,"wires":[[],[],[]]},{"id":"a85816385af348e7","type":"function","z":"793c0c2e9751236e","name":"function 1","func":"msg.payload = ` -t _hap._tcp | grep \"${msg.payload}\" | wc -l`\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":195,"wires":[["d13e98cb53d509d6"]]},{"id":"0e6a1b19b997b779","type":"subflow:793c0c2e9751236e","z":"fa8c241adfe1f04a","name":"","x":495,"y":565,"wires":[[]]}]

```

 ![Screenshot 2023-03-30 at 20.53.19](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/68e78eaac923351fc7c063a0730092a85886c26c.png)

EDIT  
The icon I used, is from a node I use - so you may need to edit the subflow

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [30 March 2023 21:56 UTC](https://discourse.nodered.org/t/exec-node-with-variables/77089/3 "2023-03-30T21:56:17Z")

</div>

Here's an alternative approach using a template to assemble the Bash command line.  
One advantage to using a template is that the complete command line is available for inspection via a debug node

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c21c1272ab8db154492c966f9e588790b808f927.jpeg)

```auto
[{"id":"4d82f796d89d6044","type":"inject","z":"db04fd79f3a93d19","name":"Pass search string msg.payload","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"03V8","payloadType":"str","x":190,"y":780,"wires":[["f66b1b60040f4067"]]},{"id":"f66b1b60040f4067","type":"template","z":"db04fd79f3a93d19","name":"Bash","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"/usr/bin/avahi-browse -t _hap._tcp | grep {{payload}} | wc -l\n","output":"str","x":410,"y":780,"wires":[["d9290c0c62e0f7c0","cc39d099b49cf9fe"]]},{"id":"d9290c0c62e0f7c0","type":"exec","z":"db04fd79f3a93d19","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":710,"y":780,"wires":[["af8dbd68b05f6313"],["af8dbd68b05f6313"],[]]},{"id":"cc39d099b49cf9fe","type":"debug","z":"db04fd79f3a93d19","name":"Full Script","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":560,"y":820,"wires":[]},{"id":"af8dbd68b05f6313","type":"debug","z":"db04fd79f3a93d19","name":"output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":780,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![lorccan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lorccan](https://discourse.nodered.org/u/lorccan)\
**Post date:** [30 March 2023 22:25 UTC](https://discourse.nodered.org/t/exec-node-with-variables/77089/5 "2023-03-30T22:25:21Z")

</div>

Thanks. For some reason I was unable to import the whole flow - I just got the 'avahi-browse' node. It was probably to do with the missing icon that you mentioned. Anyway, I have a solution that works for me now.

---

<div class="post-metadata">

**Author:** ![lorccan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lorccan](https://discourse.nodered.org/u/lorccan)\
**Post date:** [30 March 2023 22:26 UTC](https://discourse.nodered.org/t/exec-node-with-variables/77089/6 "2023-03-30T22:26:18Z")

</div>

Thank you. That's just what I needed.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 May 2023 22:26 UTC](https://discourse.nodered.org/t/exec-node-with-variables/77089/7 "2023-05-29T22:26:39Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
