# Bash commands and the exec node

**URL:** <https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087>\
**Category:** General\
**Created:** [31 January 2021 05:36 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087 "2021-01-31T05:36:43Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![prf](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@prf](https://discourse.nodered.org/u/prf)\
**Post date:** [31 January 2021 05:36 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/1 "2021-01-31T05:36:43Z")

</div>

Flushed with some mild success, I thought I'd try something else before I give it away for the day.

I've been able to run a simple Python script that prints a number series and now I'm wondering if I can run some bash commands. If I enter echo "test" on the Command line of the exec node then test is displayed as an output from the debug node. Say I want to display my pi directory with the commands:

cd /home/pi  
ls

How do I enter those two commands into the exec node? Searching the forum hasn't revealed an answer.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [31 January 2021 06:06 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/2 "2021-01-31T06:06:39Z")

</div>

Well, I guess you could do that.

There is/are better ways of doing what you want though.

Without wanting to pry too much, what exactly do you want to do?

# edit

For the sake of answering your question:

You would not do the two commands, but more:  
`ls /home/pi`  
which gets around needing two lines.

Just connect an `inject` node to the `exec` node, then connect a `debug` node to the top output of the `exec` node.

Open the `exec` node and untick the `include msg.payload` button.

Enter `ls /home/pi` in the line where you put the command.

Save that and `deploy`.

Press the `inject` node and you should see a list of files/directories from `/home/pi`

---

<div class="post-metadata">

**Author:** ![prf](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@prf](https://discourse.nodered.org/u/prf)\
**Post date:** [31 January 2021 06:32 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/3 "2021-01-31T06:32:10Z")

</div>

Silly me, I should have thought of "ls /home/pi", it's been a long hot day. Still, is it possible to enter multiple commands?

Regarding my motive, I have nothing in mind, just experimenting. What is the better way to run a Python script. I notice that there is a python shell in the contrib library, is that what you have in mind?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [31 January 2021 06:34 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/4 "2021-01-31T06:34:21Z")

</div>

Nothing wrong with just trying things. I do that a lot.

But saying that, it isn't possible to do multiple commands.

(Why would you?)

If the scenario arises where you need to do multiple commands:  
make a `script` file. (.sh) and run it.

So..... eg:

`/home/pi/something.sh`

with that file being executable and correctly structured.

---

<div class="post-metadata">

**Author:** ![prf](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@prf](https://discourse.nodered.org/u/prf)\
**Post date:** [31 January 2021 06:47 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/5 "2021-01-31T06:47:47Z")

</div>

A shell script, of course.

One last question. Is there a node that displays text in a box? For example, instead of feeding exec into a debug node could the text be properly formatted? There doesn't appear to be anything suitable in the dashboard list of nodes

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [31 January 2021 06:50 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/6 "2021-01-31T06:50:10Z")

</div>

If you click on the debug message you can "expand" the message and it is (will be) displayed properly.

Where you are now, you are more looking at the EDIT part rather than the GUI side of things.

Don't get too bogged down on what you see here as it is not really going to be useful when you are looking at a GUI.

---

<div class="post-metadata">

**Author:** ![prf](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@prf](https://discourse.nodered.org/u/prf)\
**Post date:** [31 January 2021 06:54 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/7 "2021-01-31T06:54:26Z")

</div>

I see what you mean with the debug output.

I'm more interested in displaying text in a GUI box, even though I don't a real reason to do so at the moment.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [31 January 2021 06:57 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/8 "2021-01-31T06:57:52Z")

</div>

Then you use a `TEXT` node and send the message into it. I think.

Sorry, I am a bit out of touch with that sort of stuff only that I haven't really done it AFAIK.

I'm just now diving into Python stuff so I am maybe (probably?) not the best person to really help you.

Don't be afraid to try things. Try the `text` node. But to do that, you need to make a `dashboard` and set all that up.

Not rocket science, but it takes a bit of doing.  
(Sorry, the words aren't coming out as I want just now.)

---

<div class="post-metadata">

**Author:** ![prf](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@prf](https://discourse.nodered.org/u/prf)\
**Post date:** [31 January 2021 07:01 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/9 "2021-01-31T07:01:52Z")

</div>

I had already tried the text node but it doesn't seem to do what I have in mind.

Anyway, thank you for answering my questions.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [31 January 2021 07:02 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/10 "2021-01-31T07:02:56Z")

</div>

Yeah, sorry. I have never needed to do what you want so ......

🤷‍♂️

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [31 January 2021 09:59 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/11 "2021-01-31T09:59:42Z")

</div>

1. You can concatenate command in linux using && so `cd /home/pi && ls` should work

2. You can use a ui\_template node - set the size as you require - and change the default template to be

`<pre><div ng-bind-html="msg.payload"></div></pre>`  
The template expects html - but adding pre means the carriage returns in the text are then honoured.

example flow

```auto
[{"id":"97d389df.a74008","type":"inject","z":"78444ab3.45a654","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":200,"y":540,"wires":[["8d5af63e.ae2ef8"]]},{"id":"8d5af63e.ae2ef8","type":"exec","z":"78444ab3.45a654","command":"cd ~ && ls -l","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":390,"y":540,"wires":[["7f4d00cc.09979"],[],[]]},{"id":"7f4d00cc.09979","type":"ui_template","z":"78444ab3.45a654","group":"a223b12f.6e05a","name":"","order":0,"width":"6","height":"8","format":"<pre><div ng-bind-html=\"msg.payload\"></div></pre>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":580,"y":540,"wires":[[]]},{"id":"a223b12f.6e05a","type":"ui_group","name":"Default","tab":"45c56a9a.0646c4","order":1,"disp":true,"width":"6","collapse":false},{"id":"45c56a9a.0646c4","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [31 January 2021 10:53 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/12 "2021-01-31T10:53:40Z")

</div>

Just to clarify that `&&` only runs the second command if the first completes with a zero return code. e.g. it succeeds.

Details are here:

[https://www.gnu.org/software/bash/manual/html\_node/Lists.html](https://www.gnu.org/software/bash/manual/html_node/Lists.html)

The correct way to run multiple commands regardless of success or failure is to separate them with a `;`. Windows users should note that this works with the cmd.exe shell as well. `&&` is more commonly used because you generally _want_ subsequent commands to fail if an earlier one fails.

---

<div class="post-metadata">

**Author:** ![prf](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@prf](https://discourse.nodered.org/u/prf)\
**Post date:** [31 January 2021 22:23 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/13 "2021-01-31T22:23:51Z")

</div>

Thank you for the template example, I'll give it a test.

TotallyInformation, I've come across the use of a semicolon as a separator in the past, I'll also give that a test as well.

---

<div class="post-metadata">

**Author:** ![prf](https://avatars.discourse-cdn.com/v4/letter/p/54ee81/32.png) [@prf](https://discourse.nodered.org/u/prf)\
**Post date:** [1 February 2021 04:02 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/14 "2021-02-01T04:02:30Z")

</div>

Thank you for the example, it does exactly what I had in mind.

---

<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:** [15 February 2021 04:03 UTC](https://discourse.nodered.org/t/bash-commands-and-the-exec-node/40087/15 "2021-02-15T04:03:13Z")

</div>

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