Search code examples
azure-pipelines

Why don't arguments get passed to Bash function in Azure Pipelines?


Consider the following minimalistic pipeline:

pool:
      vmImage: 'ubuntu-latest'

steps:
  - script: |
      cat >> ~/ci_bash_env.sh <<END
        my_function() {
          echo "Arguments are: $@"
        }
      END
  - script: |
      source ~/ci_bash_env.sh
      my_function 123

I expect it to output "Arguments are: 123", but the actual output is "Arguments are: ". Why arguments don't get passed and how to fix that?

If I conduct an experiment in a clean docker container with the above function my_function placed my_env.sh and my script my_function 123 placed in my_call.sh, and then execute BASH_ENV=/my_env.sh /usr/bin/bash --noprofile --norc /my_call.sh, then I get an expected output:

Arguments are: 123


Solution

  • It turned out, I needed to prepend $@ with \, like this:

    - script: |
        cat >> ~/ci_bash_env.sh <<END
          my_function() {
            echo "Arguments are: \$@"
          }
        END
    

    to prevent variable expansion ($@) inside of cat