<p>I am trying to invoke the elements of the directory array via bash script (&amp; sed), but it breaks:</p>
<p><code>sed: -e expression #1, char 33: unknown option to `s'</code></p>
<pre><code>#!/bin/bash


directory=(one two three)


for m in &quot;${directory[@]}&quot;
do
  echo &quot;${m}&quot; ;

sed -i -e '45 s/$/\napiVersion: extensions/v1beta1\nkind: Ingress\nmetadata:\n  namespace: ${m}\n  name: ${m}-api1-ingress\n  annotations:\n    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;\n    nginx.ingress.kubernetes.io/configuration-snippet: |\n      rewrite &quot;(?i)/${m}/api1/(.*)&quot; /$1 break;\n      rewrite &quot;(?i)/${m}/api1$&quot; / break;\n  labels:\n    Name: ${m}\n    App: test-api\n    Tier: end\nspec:\n  rules:\n  - host: www.test.com\n    http:\n      paths:\n      - backend:\n          serviceName: ${m}-test-api-1\n          servicePort: 80\n        path: /${m}/api1\n\n---\n\napiVersion: extensions/v1beta1\nkind: Ingress\nmetadata:\n  namespace: ${m}\n  name: ${m}-api2-ingress\n  annotations:\n    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;\n    nginx.ingress.kubernetes.io/configuration-snippet: |\n      rewrite &quot;(?i)/${m}/api2/(.*)&quot; /$1 break;\n      rewrite &quot;(?i)/${m}/api2$&quot; / break;\n  labels:\n    Name: ${m}\n    App: test-api\n    Tier: end\nspec:\n  rules:\n  - host: www.test.com\n    http:\n      paths:\n      - backend:\n          serviceName: ${m}-test-api-2\n          servicePort: 80\n        path: /${m}/api2/g' /home/test/*.yaml;

done
</code></pre>

user515576
<p>You should be able to use the following :</p>
<pre><code>sed -i -e &quot;45 a\
apiVersion: extensions/v1beta1\n\
kind: Ingress\n\
metadata:\n\
  namespace: ${m}\n\
  name: ${m}-api1-ingress\n\
  annotations:\n\
    nginx.ingress.kubernetes.io/use-regex: \&quot;true\&quot;\n\
    nginx.ingress.kubernetes.io/configuration-snippet: |\n\
      rewrite \&quot;(?i)/${m}/api1/(.*)\&quot; /\$1 break;\n\
      rewrite \&quot;(?i)/${m}/api1$\&quot; / break;\n\
  labels:\n\
    Name: ${m}\n\
    App: test-api\n\
    Tier: end\nspec:\n\
  rules:\n\
  - host: www.test.com\n\
    http:\n\
      paths:\n\
      - backend:\n\
          serviceName: ${m}-test-api-1\n\
          servicePort: 80\n\
        path: /${m}/api1\n\
\n\
---\n\
\n\
apiVersion: extensions/v1beta1\n\
kind: Ingress\nmetadata:\n\
  namespace: ${m}\n\
  name: ${m}-api2-ingress\n\
  annotations:\n\
    nginx.ingress.kubernetes.io/use-regex: \&quot;true\&quot;\n\
    nginx.ingress.kubernetes.io/configuration-snippet: |\n\
      rewrite \&quot;(?i)/${m}/api2/(.*)\&quot; /\$1 break;\n\
      rewrite \&quot;(?i)/${m}/api2$\&quot; / break;\n\
  labels:\n\
    Name: ${m}\n\
    App: test-api\n\
    Tier: end\n\
spec:\n\
  rules:\n\
  - host: www.test.com\n\
    http:\n\
      paths:\n\
      - backend:\n\
          serviceName: ${m}-test-api-2\n\
          servicePort: 80\n\
        path: /${m}/api2&quot; \
/home/test/*.yaml
</code></pre>
<p>Note each line ending with <code>\n\</code> : the <code>\n</code> is there for <code>sed</code> to add a linefeed, while the <code>\</code> is there to escape the linefeed that follows and have <code>bash</code> parse it as decorative and avoid passing it to <code>sed</code> which would consider it as the end of the <code>a</code>ppend command.</p>
<p>Also note the need to escape the double-quotes in the data so that they aren't considered as closing the quotes that contain the whole data.</p>
<p>Given the quantity of data and the presence of dynamically generated data, it would be preferable to store the generic data in its own file, have a first pass on that file to replace the variables with their values and then use <code>sed</code> or other to insert the whole transformed file (command <code>w filename</code> with <code>sed</code>). That way you wouldn't have to bother with every character your data contains that could be parsed as a meta-character as the data in the file would automatically be parsed as no more than data.</p>
