title: Howto lint a bash script inside a YAML file
body: <p>I am trying to create a GitLab CI/CD job that takes a bash script from a YAML file and checks if the syntax is correct.</p>
  
  <p>I have a YAML file called <code>.gitlab-ci.template.yml</code> with the following content:</p>
  
  <pre><code>image: node:10.15.3-alpine
  
  stages:
    - deploy
  
  deploy:
    stage: deploy
    script:
      - apk_install
      - yarn_install
      - build
      - deploy
  
  .bash: &amp;bash |
    function apk_install() {
      apk add jq git curl grep
    }
  
    function yarn_install() {
       yarn
    }
    function build() {
      echo "Build"
  
      if [ -f ".gitlab-ci.template.yml" ]; then
         echo "Just a dumb test for more lines"
      fi 
    }
  
    function deploy() {
      echo "Deploy"
    }
  
  before_script:
    - *bash
  </code></pre>
  
  <p>I would like to take the <code>bash</code> part and test it. I installed <code>shyaml</code> to get values from the YAML file, like so:</p>
  
  <pre><code>FILE=`cat .gitlab-ci.template.yml | shyaml get-value before_script`
  </code></pre>
  
  <p>The contents of <code>$FILE</code> would be:</p>
  
  <pre><code>- "function apk_install() {\n apk add jq git curl grep\n}\n\nfunction yarn_install()\ \ {\n yarn\n}\nfunction build() {\n echo \"Build\"\n \n if [ -f \".gitlab-ci.template.yml\"\ \ ]; then\n echo \"Just a dumb test for more lines\"\n fi\n}\n\nfunction deploy()\ \ {\n echo \"Deploy\"\n}\n"
  </code></pre>
  
  <p>Then with some the following command I try to get a valid file again:</p>
  
  <pre><code>echo $FILE | xargs -0 printf '%b\n' | sed 's/\\[[:space:]]\\[[:space:]]/ /g' | sed 's/\\"/"/g' | sed 's/\\[[:space:]]//g'
  </code></pre>
  
  <p>Now I can remove some chars from the beginning and end. But I was wondering would there be any better/easier way?</p>