# Template problem with bash array length syntax

**URL:** <https://forum.ansible.com/t/template-problem-with-bash-array-length-syntax/13304>\
**Category:** Ansible Project\
**Created:** [August 14, 2013, 3:41pm UTC](https://forum.ansible.com/t/template-problem-with-bash-array-length-syntax/13304 "2013-08-14T15:41:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dmitry\_Horbach](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@Dmitry\_Horbach](https://forum.ansible.com/u/Dmitry_Horbach)\
**Post date:** [August 14, 2013, 3:41pm UTC](https://forum.ansible.com/t/template-problem-with-bash-array-length-syntax/13304/1 "2013-08-14T15:41:51Z")

</div>

Hi,

Having simple template file with bash valid content like:

> ${#ArrayName[@]}

will break template module:

- template: src=template dest=template2

TASK: [template src=template dest=template2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [127.0.0.1] =\> {‘msg’: ‘Missing end of comment tag’, ‘failed’: True}  
fatal: [127.0.0.1] =\> {‘msg’: ‘Missing end of comment tag’, ‘failed’: True}

Is there any workaround for this?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [August 14, 2013, 3:48pm UTC](https://forum.ansible.com/t/template-problem-with-bash-array-length-syntax/13304/2 "2013-08-14T15:48:22Z")

</div>

{# #} is a jinja comment tag which is why it is gettitng confused here  
try:  
{{ ‘${#ArrayName[@]}’ }}

---

<div class="post-metadata">

**Author:** ![Dmitry\_Horbach](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@Dmitry\_Horbach](https://forum.ansible.com/u/Dmitry_Horbach)\
**Post date:** [August 14, 2013, 3:57pm UTC](https://forum.ansible.com/t/template-problem-with-bash-array-length-syntax/13304/3 "2013-08-14T15:57:17Z")

</div>

Works great! Thanks a lot Brian.

---

<div class="post-metadata">

**Author:** ![ric1](https://avatars.discourse-cdn.com/v4/letter/r/74df32/32.png) [@ric1](https://forum.ansible.com/u/ric1)\
**Post date:** [August 14, 2013, 6:48pm UTC](https://forum.ansible.com/t/template-problem-with-bash-array-length-syntax/13304/4 "2013-08-14T18:48:45Z")

</div>

You can also wrap an entire block text/code in {% raw %} {% endraw %}:

{% raw %}  
for (( 1=0; i\<$#allowed\_list[@]}; i++ )); do  
allowed=${allowed\_list[$i]

# do something here with $allowed

done  
{% endraw %}

This allows you to avoid escaping all the dollar signs, as well…

-Ric

---

<div class="post-metadata">

**Author:** ![einar.coutin](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@einar.coutin](https://forum.ansible.com/u/einar.coutin)\
**Post date:** [May 24, 2016, 7:12pm UTC](https://forum.ansible.com/t/template-problem-with-bash-array-length-syntax/13304/5 "2016-05-24T19:12:06Z")

</div>

It gets even more interesting if you get something like this:  
define(‘AUTH\_KEY’, 'TccKCkdkq.}]I]I?$(.R@L)ASUysg% AB(a5j+`A8\_ra6Vu7D\*h2Q]||}.{**#b}5Xq’);**

Because the ; makes the engine throw this error:  
expected token ‘end of print statement’, got ‘;’

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [June 10, 2016, 2:39pm UTC](https://forum.ansible.com/t/template-problem-with-bash-array-length-syntax/13304/6 "2016-06-10T14:39:18Z")

</div>

Hit something similar when pushing go templates out with the ansible  
template command.

Use

{%raw%}  
..... line noise goes here ....  
{%endraw%}
