# shell script gives different output when invoked using Ansible shell module.

**URL:** <https://forum.ansible.com/t/shell-script-gives-different-output-when-invoked-using-ansible-shell-module/31196>\
**Category:** Ansible Project\
**Created:** [September 4, 2019, 5:45am UTC](https://forum.ansible.com/t/shell-script-gives-different-output-when-invoked-using-ansible-shell-module/31196 "2019-09-04T05:45:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 4, 2019, 5:45am UTC](https://forum.ansible.com/t/shell-script-gives-different-output-when-invoked-using-ansible-shell-module/31196/1 "2019-09-04T05:45:46Z")

</div>

I have check.sh script that I wish to run on the target nodes:

`  
cat check.sh

str=`echo $1 | sed -e 's#[\][\]n# #g'`  
echo $str\>check.row  
`

It is suppose to replace \n with a single white space from the argument and save it in check.row file.

When I run it manually on the target server i get good output results as shown below:

> bash -x ./check.sh ‘/fin/app/01/scripts\n/fin/app/01/sql’  
> ++ echo ‘/fin/app/01/scripts\n/fin/app/01/sql’  
> ++ sed -e ‘s#n# #g’
> 
> - str=‘/fin/app/01/scripts /fin/app/01/sql’
> - echo /fin/app/01/scripts /fin/app/01/sql

Here is the check.row file output:

> [user1@remotehost1 ~]$ cat check.row  
> /fin/app/01/scripts /fin/app/01/sql

However, when i run the same using ansible shell or command module I do not get the expected results.

Below is my playbook:

`  
tasks:

- copy:  
src: “{{ playbook\_dir }}/files/check.sh”  
dest: “~/”  
mode: 0754

- set\_fact:  
install\_dir: “{{ hostvars['localhost']['command\_result'].stdout.split(‘\t’)[2] }}”

- shell: “bash -x ~/check.sh ‘{{ install\_dir }}’ \> ~/check\_rollback.log”  
`

See ansible’s debug output below:

> changed: [10.8.44.55] =\> {  
> “changed”: true,  
> “cmd”: “bash -x ~/check.sh ‘/fin/app/01/scripts\n/fin/app/01/sql’ \> ~/check\_rollback.log”,  
> “delta”: “0:00:00.118943”,  
> “end”: “2019-09-04 10:50:16.503745”,  
> “invocation”: {  
> “module\_args”: {  
> “\_raw\_params”: “bash -x ~/check.sh ‘/fin/app/01/scripts\n/fin/app/01/sql’ \> ~/check\_rollback.log”,  
> “\_uses\_shell”: true,  
> “argv”: null,  
> “chdir”: null,  
> “creates”: null,  
> “executable”: null,  
> “removes”: null,  
> “stdin”: null,  
> “stdin\_add\_newline”: true,  
> “strip\_empty\_ends”: true,  
> “warn”: true  
> }  
> },  
> “rc”: 0,  
> “start”: “2019-09-04 10:50:16.384802”,  
> “stderr”: “++ echo ‘/fin/app/01/scripts\n/fin/app/01/sql’\n++ sed -e ‘s#[\][\]n# #g’\n+ str=‘/fin/app/01/scripts\n/fin/app/01/sql’\n+ echo ‘/fin/app/01/scripts\n/fin/app/01/sql’”,  
> “stderr\_lines”: [  
> “++ echo ‘/fin/app/01/scripts\n/fin/app/01/sql’”,  
> “++ sed -e ‘s#[\][\]n# #g’”,  
> “+ str=‘/fin/app/01/scripts\n/fin/app/01/sql’”,  
> “+ echo ‘/fin/app/01/scripts\n/fin/app/01/sql’”  
> ],  
> “stdout”: “”,  
> “stdout\_lines”:   
> }

And here is the check.row file output:

> [user1@remotehost1 ~]$ cat check.row  
> /fin/app/01/scripts\n/fin/app/01/sql

As you can instead of single whitespace it is now printing \n.

One can replicate this issue easily. Can you please suggest why am I getting this issue and how to fix this?

---

<div class="post-metadata">

**Author:** ![Fernando\_Scarlazzari](https://avatars.discourse-cdn.com/v4/letter/f/e19b73/32.png) [@Fernando\_Scarlazzari](https://forum.ansible.com/u/Fernando_Scarlazzari)\
**Post date:** [October 9, 2019, 11:12am UTC](https://forum.ansible.com/t/shell-script-gives-different-output-when-invoked-using-ansible-shell-module/31196/2 "2019-10-09T11:12:47Z")

</div>

Mohtashim,

Try using “echo **-e** ”.
