# "here doc" in shell command

**URL:** <https://forum.ansible.com/t/here-doc-in-shell-command/25751>\
**Category:** Ansible Project\
**Created:** [October 16, 2017, 4:43am UTC](https://forum.ansible.com/t/here-doc-in-shell-command/25751 "2017-10-16T04:43:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rurpy](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rurpy](https://forum.ansible.com/u/rurpy)\
**Post date:** [October 16, 2017, 4:43am UTC](https://forum.ansible.com/t/here-doc-in-shell-command/25751/1 "2017-10-16T04:43:33Z")

</div>

I am trying to use a “here doc” in a shell command like so:

- name: frustration  
shell: |  
cat \<\<-EOF | my\_program  
input line 1  
input line 2  
EOF  
another\_program

along with a number of permutations, all of which have resulted in “bash: line 3: warning: here-document at line 0 delimited by end-of-file (wanted `EOF’)”.

I found this:  
[https://github.com/ansible/ansible/issues/12856](https://github.com/ansible/ansible/issues/12856)  
which seems to imply there is some way to accomplish what I’m trying do but I can’t understand how, nor do I see anything else relevant in the Ansible docs or Google.

What is the magic syntax for the above?

---

<div class="post-metadata">

**Author:** ![thomas.lehmann](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@thomas.lehmann](https://forum.ansible.com/u/thomas.lehmann)\
**Post date:** [October 16, 2017, 11:12am UTC](https://forum.ansible.com/t/here-doc-in-shell-command/25751/2 "2017-10-16T11:12:40Z")

</div>

`

- name: fun fun fun  
shell:  
cmd: |  
#!/bin/bash  
SCRIPT=$(cat \<\<EOF  
echo “hello world!”  
EOF  
)  
${SCRIPT}

`

Solution found here: [https://stackoverflow.com/questions/40230184/how-to-do-multiline-shell-script-in-ansible](https://stackoverflow.com/questions/40230184/how-to-do-multiline-shell-script-in-ansible)

---

<div class="post-metadata">

**Author:** ![rurpy](https://avatars.discourse-cdn.com/v4/letter/r/919ad9/32.png) [@rurpy](https://forum.ansible.com/u/rurpy)\
**Post date:** [October 16, 2017, 10:22pm UTC](https://forum.ansible.com/t/here-doc-in-shell-command/25751/3 "2017-10-16T22:22:11Z")

</div>

Ah, thank you very much! I actually had seen that SO post before but realize now when I tried it I screwed up (I had left the vertical bar character on the “shell” line, not the command line.) Now I see it does in fact work.

But one further question… Where is “cmd” documented?

---

<div class="post-metadata">

**Author:** ![thomas.lehmann](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@thomas.lehmann](https://forum.ansible.com/u/thomas.lehmann)\
**Post date:** [October 18, 2017, 7:48am UTC](https://forum.ansible.com/t/here-doc-in-shell-command/25751/4 "2017-10-18T07:48:59Z")

</div>

Good point. It’s seems not officially documented. In my local Ansible installation I’ve found the relating code part  
in playbook/tasks.py

`

182 # the command/shell/script modules used to support the `cmd` arg,  
183 # which corresponds to what we now call \_raw\_params, so move that  
184 # value over to \_raw\_params (assuming it is empty)  
185 if action in (‘command’, ‘shell’, ‘script’):  
186 if ‘cmd’ in args:  
187 if args.get(‘\_raw\_params’, ‘’) != ‘’:  
188 raise AnsibleError(“The ‘cmd’ argument cannot be used when other raw parameters are specified.”  
189 " Please put everything in one or the other place.", obj=ds)  
190 args[‘\_raw\_params’] = args.pop(‘cmd’)

`
