# How to use piping multiple shell commands

**URL:** <https://forum.ansible.com/t/how-to-use-piping-multiple-shell-commands/30926>\
**Category:** Ansible Project\
**Created:** [August 1, 2019, 3:01am UTC](https://forum.ansible.com/t/how-to-use-piping-multiple-shell-commands/30926 "2019-08-01T03:01:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 1, 2019, 3:01am UTC](https://forum.ansible.com/t/how-to-use-piping-multiple-shell-commands/30926/1 "2019-08-01T03:01:19Z")

</div>

Hi,

I am trying the below snippet to detect the established ports. but Ansible throws an error.

`

- name: List the port connections  
shell: "netstat -nautp |grep -iE “137|389|445|636” |awk ‘{print $5}’ |cut -d: -f2 |grep -iwE “137|389|445|636”  
register: dc\_stat  
debug:  
var: dc\_stat.stdout\_lines  
`

this through’s the error

`  
The offending line appears to be:

- name: List the port connections  
shell: netstat -nautp |grep -iE “137|389|445|636” |awk ‘{print $5}’ |cut -d: -f2 |grep -iwE “137|389|445|636”  
^ here  
We could be wrong, but this one looks like it might be an issue with  
unbalanced quotes. If starting a value with a quote, make sure the  
line ends with the same set of quotes. For instance this arbitrary  
example:

foo: “bad” “wolf”

Could be written as:

foo: ‘“bad” “wolf”’

`

I tried inserted a " " for the shell commands and using vars/pipe also …  
How can i direct the output of "netstat -nautp |grep -iE “137|389|445|636” |awk ‘{print $5}’ |cut -d: -f2 |grep -iwE “137|389|445|636"” to a variable ?

Or do we have to use backslash() as in shell ?

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [August 1, 2019, 3:27am UTC](https://forum.ansible.com/t/how-to-use-piping-multiple-shell-commands/30926/2 "2019-08-01T03:27:59Z")

</div>

Error clearly says to use proper quotes

‘netstat -nautp |grep -iE “137|389|445|636” |awk ‘{print $5}’ |cut -d: -f2 |grep -iwE “137|389|445|636”’

Save it in Regiter  
Use set\_facts to store as a variable

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [August 1, 2019, 3:29am UTC](https://forum.ansible.com/t/how-to-use-piping-multiple-shell-commands/30926/3 "2019-08-01T03:29:15Z")

</div>



---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [August 1, 2019, 12:57pm UTC](https://forum.ansible.com/t/how-to-use-piping-multiple-shell-commands/30926/4 "2019-08-01T12:57:21Z")

</div>

Thanks Vinoth. It worked

Yes . I learnt that we can use the formatting as in python either with ’ ’ or " ".
